Laravel 7 Guzzle Http Client Request Example

29698 views 2 years ago Laravel

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 ken how to utilize laravel 6 guzzle http client request? If you don't ken how to utilize it, then you are a right place.

A Guzzle is a PHP HTTP client that makes it facile to send HTTP requests with data, headers and nugatory to integrate with web accommodations. Guzzle is a simple interface for building query strings, POST requests, streaming astronomically immense uploads, streaming immensely colossal downloads, utilizing HTTP cookies, uploading JSON data, etc.

In this tutorial i will show you GET, POST, PUT and EXPUNGE request of guzzle http client. Just you have to consummate this tutorial from up to bottom. Then you will ken how to utilize guzzle http request.

I am going to utilize guzzlehttp/guzzle composer package for guzzle http request in laravel 6 application. Let's visually perceive how we can utilize guzzle http request in our laravel 6 application.

You can additionally read the guzzle official github documentation to follow this link

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:

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'] = "LaravelCode";
    $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'] = "LaravelCode";
    $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);
}

i hope you like this one.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]