Laravel Http Curl Get Request Example

By Hardik Savani November 5, 2023 Category : Laravel

This example is focused on laravel http curl get request example. We will look at example of laravel curl get request example. This post will give you simple example of laravel http request get parameters. let’s discuss about how to call curl get request in laravel. Here, Creating a basic example of how to get curl request in php laravel.

Here, i will give you two examples of how to call curl get request with laravel GuzzleHttp. first example will with http and second example with GuzzleHttp. so let's see both examples one by one here.

Install guzzlehttp/guzzle Package:

you have to install guzzlehttp/guzzle composer package in your project:

composer require guzzlehttp/guzzle

Example 1:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Http;

class ITSController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$apiURL = 'https://api.mywebtuts.com/api/users';

$parameters = ['page' => 2];

$response = Http::get($apiURL, $parameters);

$statusCode = $response->status();

$responseBody = json_decode($response->getBody(), true);

dd($responseBody);

}

}

Output

Array

(

[current_page] => 2

[data] => Array

(

[0] => Array

(

[id] => 7

[first_name] => Howell

[last_name] => Funke

[email] => howellfunke@gmail.com

[avatar] => https://api.mywebtuts.com/asset/img/photo-7.png

)

[1] => Array

(

[id] => 8

[first_name] => Edwards

[last_name] => Lindsay

[email] => edwardslindsay@gmail.com

[avatar] => https://api.mywebtuts.com/asset/img/photo-9.png

)

...........

...........

Example 2:

<?php

namespace App\Http\Controllers;

class ITSController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index2()

{

$apiURL = 'https://api.mywebtuts.com/api/users';

$parameters = ['page' => 2];

$client = new \GuzzleHttp\Client();

$response = $client->request('GET', $apiURL, ['query' => $parameters]);

$statusCode = $response->getStatusCode();

$responseBody = json_decode($response->getBody(), true);

dd($responseBody);

}

}

Output

Array

(

[current_page] => 2

[data] => Array

(

[0] => Array

(

[id] => 7

[first_name] => Howell

[last_name] => Funke

[email] => howellfunke@gmail.com

[avatar] => https://api.mywebtuts.com/asset/img/photo-7.png

)

[1] => Array

(

[id] => 8

[first_name] => Edwards

[last_name] => Lindsay

[email] => edwardslindsay@gmail.com

[avatar] => https://api.mywebtuts.com/asset/img/photo-9.png

)

...........

...........

i hope it can help you...

Tags :
Shares