From the version of Laravel 8, Laravel default uses guzzlehttp/guzzle and Laravel automatically includes this dependency. So no need to install this in our Laravel application by running the composer require command.

Laravel provides an expressive, minimal API around the Guzzle HTTP client, allowing us to make HTTP requests to communicate with another web applications

In this tutorial, we will see the guzzle http laravel get request example using Laravel Http client. We will use json placeholder api for testing purposes and you know that this api is for developers.

So let's see the example code of laravel 10 HTTP get request example:

laravel-10-guzzle-http-get-request-example

Step 1: Install Laravel 10

First of all, we need to get a fresh Laravel 10 version application using the bellow command, So open your terminal OR command prompt and run the bellow command to start How to get Http response in Laravel.

composer create-project laravel/laravel example-app

 

Step 2: Create Route

Here, we need to add routes to display the Http client request data. 

routes/web.php

<?php

use App\Http\Controllers\HttpController;
use Illuminate\Support\Facades\Route;

Route::get('http_get_request', [HttpController::class, 'fetch']);

 

Step 3: Create Controller

Here, now we have to create a HttpController to process our api request. So create it and update it like below:

app/Http/Controllers/HttpController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Http;

class HttpController extends Controller
{
    public function fetch()
    {
        $response = Http::get('https://jsonplaceholder.typicode.com/todos/1');

        return $response->body();
    }
}

 

Now if you start your server by running php artisan serve and visit the below url:

URL
http://127.0.0.1:8000/http_get_request

 

Then you will see the following output:

{ 
    "userId": 1, 
    "id": 1, 
    "title": "delectus aut autem", 
    "completed": false 
}

 

This guzzle Http get request response returns an instance of Illuminate\Http\Client\Response, which provides a variety of methods that may be used to inspect the response:

$response->body() : string;
$response->json($key = null, $default = null) : array|mixed;
$response->object() : object;
$response->collect($key = null) : Illuminate\Support\Collection;
$response->status() : int;
$response->successful() : bool;
$response->redirect(): bool;
$response->failed() : bool;
$response->clientError() : bool;
$response->header($header) : string;
$response->headers() : array;

 

Now you can directly get the response data like:

return Http::get('http://example.com/users/1')['name'];

 

There is an optional option to use Http get request call URI template like:

Http::withUrlParameters([
    'endpoint' => 'https://www.laravelia.com',
    'page' => 'api',
    'version' => '1.x',
    'topic' => 'api',
])->get('{+endpoint}/{page}/{version}/{topic}');

 

Read also: Laravel 10 Razorpay Payment Gateway Integration

 

Conclusion

You know how to use guzzle http laravel. Hope after following this how to get Http response in Laravel, you own now be able to create this laravel http request.

Category : #laravel

Tags : #laravel , #laravel api