Laravel 8.x Guzzle Http Client Request Example

Hello Artisan, in this tutorial i am going to discuss about guzzle http client request. I will discuss about guzzle http client request step by step. Do you know how to use laravel 8 guzzle http client request? If you don't know how to use it, then you are a right place.

A Guzzle is a PHP HTTP client that makes it easy to send HTTP requests with data, headers and trivial to integrate with web services. Guzzle is a simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc.

In this tutorial i will show you GET, POST, PUT and DELETE request of guzzle http client. Just you have to complete this tutorial from up to bottom. Then you will know how to use guzzle http request.

I am going to use guzzlehttp/guzzle composer package for guzzle http request in laravel 8 application. Let's see how we can use guzzle http request in our laravel 8 application.

You can also read the guzzle official github documentation to follow this link guzzle http client request.

laravel-6-guzzle-http-client-request-example

 

Step 1 : Install Guzzle Package:

Now we need to install guzzlehttp/guzzle package in our application so that we can use it. So let's run the below command.

composer require guzzlehttp/guzzle

 

Step 2 : Demo Requests Example Using Guzzle:

 

Read also : Laravel 6 REST API with JWT Authentication with CRUD

Now here i will show you one after another how to use all http request using guzzle.

GET REQUEST

public function getGuzzleRequest()
{
    $client = new \GuzzleHttp\Client();
    $request = $client->get('http://example.com');
    $response = $request->getBody();
   
    dd($response);
}

 

POST REQUEST

public function postGuzzleRequest()
{
    $client = new \GuzzleHttp\Client();
    $url = "http://example.com/api/posts";
   
    $data['name'] = "codechief";
    $request = $client->post($url,  ['body'=>$data]);
    $response = $request->send();
  
    dd($response);
}

 

PUT REQUEST

public function putGuzzleRequest()
{
    $client = new \GuzzleHttp\Client();
    $url = "http://example.com/api/posts/1";
    $data['name'] = "codechief";
    $request = $client->put($url,  ['body'=>$data]);
    $response = $request->send();
   
    dd($response);
}

 

DELETE REQUEST:

public function deleteGuzzleRequest()
{
    $client = new \GuzzleHttp\Client();
    $url = "http://example.com/api/posts/1";
    $request = $client->delete($url);
    $response = $request->send();
  
    dd($response);
}

 

Now hope you know how to use guzzle http client in your laravel application. Hope this tutorial will help you.

 

author-image
Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js