ItSolutionStuff.com

Laravel CURL Request Example using Ixudra/curl

By Hardik Savani • November 5, 2023
Laravel

If you know about php curl then you can simply run get, post request and get data from url. php curl is very interesting things. if you know and use in with php then you have to do curl_ini(), curl_setopt(), curl_exec() and curl_close() etc that way we can simply run request.

But, If you require to fire curl request in laravel 5 application then you don't require to do curl_init(), curl_close() etc. We can run request very easily and get response in json. so we will use ixudra/curl composer package for run curl request very simple.

Using ixudra curl package you can very simply curl post request in laravel, curl get request in laravel, curl put request in laravel, curl delete request in laravel etc. So here you can also set header, data etc. Here first i will install package on laravel application then we will give you very simple example to get json data. So let's just follow bellow example:

Install ixudra/curl Package:

First of all, we have to add ixudra/curl package for run curl request method so one your cmd or terminal and fire bellow command:

composer require ixudra/curl

After successfully install package, open config/app.php file and add service provider and alias.

config/app.php

'providers' => [

....

Ixudra\Curl\CurlServiceProvider::class,

],

'aliases' => [

....

'Curl' => Ixudra\Curl\Facades\Curl::class,

]

Add New Route:

next we need to create route for run get curl request. so open your routes/web.php file and add following route.

routes/web.php

Route::get('get-curl', 'HomeController@getCURL');

Add Controller Method:

Here, we will add new getCURL() in HomeController so if you don't have HomeController then create and add following method.

app/Http/Controllers/HomeController.php

<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;

use Ixudra\Curl\Facades\Curl;


class HomeController extends Controller

{


/**

* Show the application dashboard.

*

* @return \Illuminate\Http\Response

*/

public function getCURL()

{

$response = Curl::to('https://jsonplaceholder.typicode.com/posts')

->get();


dd($response);

}

}

Now, you can simply run above example and see you will get result 100 data in json, it is very simple as you see, you don't require to anymore use php curl and very easily run curl request.

So you can also run post, put, patch, delete request as bellow example, let's just need to change getCURL() on home page:

CURL Post Request:

public function getCURL()

{

$response = Curl::to('https://example.com/posts')

->withData(['title'=>'Test', 'body'=>'sdsd', 'userId'=>1])

->post();

dd($response);

}

CURL Put Request:

public function getCURL()

{

$response = Curl::to('https://example.com/posts/1')

->withData(['title'=>'Test', 'body'=>'sdsd', 'userId'=>1])

->put();

dd($response);

}

CURL Patch Request:

public function getCURL()

{

$response = Curl::to('https://example.com/posts/1')

->withData(['title'=>'Test', 'body'=>'sdsd', 'userId'=>1])

->patch();

dd($response);

}

CURL Delete Request:

public function getCURL()

{

$response = Curl::to('https://example.com/posts/1')

->delete();

dd($response);

}

As above requests we can do it very simply. So you can also get more information about curl package from here : ixudra/curl.

I hope you will find your solution.

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Add Soft Delete to Existing Table Example

Read Now →

Laravel Delete File After Download Response Example

Read Now →

Laravel Ajax DELETE Request Example Tutorial

Read Now →

Laravel Cookies - Get, Set, Delete Cookie Example

Read Now →

Laravel Install Tailwind CSS Example

Read Now →

How to Add Google Map in Laravel?

Read Now →

Laravel Http Curl Post Request with Headers Example

Read Now →

Laravel Http Curl Delete Request Example

Read Now →

Laravel Http Curl Get Request Example

Read Now →

Laravel Carbon Get All Dates Between Two Dates Example

Read Now →

How to Use MySQL View in Laravel?

Read Now →

Laravel Unique Validation With Soft Delete Example

Read Now →

Laravel Guzzle Http Client POST Request Example

Read Now →

Laravel Elasticsearch with Pagination Example

Read Now →

Laravel Custom Pagination View Example

Read Now →