Laravel curl request using ixudra/curl package

Have a look some curl built in function

curl_init();      // initializes a cURL session
curl_setopt();    // changes the cURL session behavior with options
curl_exec();      // executes the started cURL session
curl_close();     // closes the cURL session and deletes the variable made by curl_init();
PHP

cURL GET request example code

ou can test cURL in your own local server as it’s the same with using a regular form with an action.

$url = "https://learnwebcode.github.io/json-example/animals-1.json";

//  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

// Print the return data
print_r(json_decode($result, true));
PHP

Use json_decode if the return is in json format. 

But in this tutorial i am going to show you curl request example in laravel 6. But, If you require to fire curl request in laravel 6 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.

We can also fetch data from url in JavaScript using XMLHttpRequest. See the below example 

window.onload = function(){
    
    var request = new XMLHttpRequest();

    request.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            console.log(this.responseText);
        }
    };

    request.open('GET', 'https://jsonplaceholder.typicode.com/users/1');
    request.send();
}
JavaScript

 

If you run this code, then you will see the fetch data from this url. By the way, that is not our concern, cause we are going to make it in laravel. 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 let's just follow bellow example:

 

Step 1 : Install ixudra/curl Package:

First of all we have to install this package. Cause we will check it using this package. So run below command

composer require ixudra/curl
PHP

 

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,

]
PHP

 

Step 2 : Add Route:

After that 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('check_curl', 'HomeController@getData');
PHP

 

Step 3 : Add Controller Method:

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

app/Http/Controllers/HomeController.php

namespace App\Http\Controllers;


use Illuminate\Http\Request;
use Ixudra\Curl\Facades\Curl;


class HomeController extends Controller
{
    public function getData()
    {
        $response = Curl::to('https://jsonplaceholder.typicode.com/users/1')
                            ->get();


        dd($response);
    }
}
PHP

 

Now, you can simply run above example and see you will get result of this above url which we already fetch from xmlHttpRequest.

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

CURL Post Request:

public function getData()

{

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

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

                ->post();

    dd($response);

}
PHP

 

CURL Put Request:

public function getData()

{

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

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

                ->put();

    dd($response);

}
PHP

 

CURL Patch Request:

public function getData()

{

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

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

                ->patch();

    dd($response);

}
PHP

 

CURL Delete Request:

public function getData()

{

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

                ->delete();

    dd($response);

}
PHP

 

you can also get more information about curl package from here : ixudra/curl.

Hope this curl requests tutorial will help you.