HTTP Client Guzzle Package With Basic Usage In Laravel

  • 09-08-2022
  • 912
  • Laravel 9
  • Haresh Chauhan

Laravel HTTP Client, Laravel 8 HTTP Client Introduction, In this tutorial We will get a little knowledge on how to install the Guzzle HTTP client, How it works in the laravel application, We will focus on the new HTTP Client request. Laravel creator after the 7th version too many be improvised to take implementation to the new HTTP Client very easy and time-saving process for the developing purpose.

To make HTTP Client third-party request from the laravel application using HTTP Client, It is very easy to install and simple process to send the HTTP request to the other server. The objective of the HTTP Client Package is to make programmers' life easier while they develop any type of application.

Guzzle package installation, Throughout this tutorial example, we will use PHP 7.4 version and laravel 8 or older version. Therefore, make sure you installed it in order to follow this tutorial from the scratch.

To staring with HTTP Client requests using the Guzzle package, We will need to install the Guzzle package in our laravel application. Now the laravel By default provides, Laravel also with ships with the Guzzle package in the latest version.

Even if you checked, not installed, or by mistake deleted this package from the application you can install it via the below given suggested command in your terminal of the package.

Guzzle package installation

To install Guzzle HTTP Client request check, IF not installed, Install via the below-given command in your terminal.

composer require guzzlehttp/guzzle

How to Make HTTP Request

Here is the basic usage of the HTTP Client request send to the other services provider server.

You just need to do the initially request using HTTP::get(), and inside the get method, we will pass the URL.

use Illuminate\Support\Facades\Http;
 
$response = Http::get('https://webappfix.com');

// Get your response body
$response = $response->body();

To get your HTTP Client request response data to JSON format, You can just add "json()" method.

$response = Http::get('https://jsonplaceholder.typicode.com/todos/1');
 
// Array of data from the JSON response
$data = $response->json();

Timeout Request

In case, If you want to make a request timeout to the sent request, You can just add "timeout(5)". It will be expired sent request within 5 seconds.

$response = Http::timeout(5)
    ->get( 'https://jsonplaceholder.typicode.com/todos/1');

// TIMEOUT AFTER 5 SECONDS

Different Methods Handling

Different methods for inspecting your response from the API:

$response = Http::get('https://jsonplaceholder.typicode.com/todos/1');

// Boolean checks on the response
$response->ok() : bool;
$response->status() : int; // To know response status code
$response->body() : string;
$response->json() : array|mixed;
$response->successful() : bool;
$response->failed() : bool;
$response->serverError() : bool;
$response->clientError() : bool;
$response->header($header) : string;
$response->headers() : array;

CRUD Example

Here is, Full CRUD operations example with the HTTP Client request, including Listing, Create, Update and delete with their specific request GET, PUT, PATCH, DELETE, POST. etc.

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;

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

       return response()->json($response);
    }

    // CREATE
    public function create(Request $request)
    {
        $response = Http::post('https://jsonplaceholder.typicode.com/todos', [
                'title' => 'Your Title',
                'body' => 'Your Body',
        ]);

        return $response;
    }

    // UPDATE PUT METHOD
    public function update(Request $request)
    {
        $response = Http::put('https://jsonplaceholder.typicode.com/todos/1', [
            'title' => 'Your Title',
        ]);
        
        return $response;
    }

    // UPDATE PATCH METHOD
    public function update(Request $request)
    {
        $response = Http::patch('https://jsonplaceholder.typicode.com/todos/1', [
                'title' => 'Your Title',
                'body' => 'Your Body',
        ]);

        return $response;
    }

    // DELETE
    public function delete(Request $request)
    {
        $response = Http::delete('https://jsonplaceholder.typicode.com/todos', [
                'title_id' => '1',
        ]);

        return $response;
    }
}
Conclusion

In this tutorial from the scratch, We have learned How to use HTTP Client in the laravel application. Also, we have seen full CRUD operation with different request methods. Also, we have the see basics of the package in the laravel application. Also, we have seen All the different methods of the request-response handled error and success response.


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :