Laravel 6 curl request example using ixudra/curl package

cURL is software which you can use to make various requests using different protocols. PHP has the option to use cURL and in this article, we’ll show several examples. In this tutorial we are going to see how we can get api data using curl get request. 

There will be times that you will need to pull out data from a web service using PHP’s GET method. In this tutorial I will be demonstrating how you can make a GET Request using cURL.

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.

laravel-curl-request-example

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();

I already did this tutorial using core php. If you don't know how cURL works in PHP, then you can read this below example.

Read also : Curl Get Request with Parameters Example

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();
}

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

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,

]

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', '[email protected]');

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);
    }
}

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);

}

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);

}

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);

}

CURL Delete Request:

public function getData()

{

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

                ->delete();

    dd($response);

}

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

Hope this curl requests tutorial will help you. 

 

Codechief is a very fast growing community among programmers and have a reach of around 1 million+ readers globally. Contribution at Codechief is open for all those who have a passion to learn and help others by sharing their knowledge. If you think you have the zeal to learn, start contributing on Codechief contribute. you can also mail your article to [email protected] See your article appearing on the codechief main page and help other code.

We believe that everyone has the right to learn, so we allow both students and professionals to contribute on Codechief.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website