HDTuto.com

Using Laravel 6 Guzzle Http Client Request Example



Hi Dev,

In this article, i will let you know how to use guzzle http client request in laravel 6 application. we can easily use laravel 6 guzzle http client request using guzzlehttp/guzzle composer package. i will give you step by step simple example of guzzle http client request in laravel 6.

we will use guzzlehttp/guzzle composer package for guzzle http request in laravel 6 application.

we can make simply http request with json data or multipart form data, also you can set header of request in guzzlehttp laravel 6.

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.

Let's see bellow step and you can get simple example.

Install Package:

now we will install guzzlehttp/guzzle package and then we can easily use thir method So let's just run bellow command.

composer require guzzlehttp/guzzle

Example of Requests Using Guzzle:

Now here i will show you how to run all above listed request you can use following controller method:

GET Request:

public function getGuzzleRequest()

{

$client = new \GuzzleHttp\Client();

$request = $client->get('http://myexample.com');

$response = $request->getBody();

dd($response);

}

POST Request:

public function postGuzzleRequest()

{

$client = new \GuzzleHttp\Client();

$url = "http://myexample.com/api/posts";

$myBody['name'] = "Demo";

$request = $client->post($url, ['body'=>$myBody]);

$response = $request->send();

dd($response);

}

PUT Request:

public function putGuzzleRequest()

{

$client = new \GuzzleHttp\Client();

$url = "http://myexample.com/api/posts/1";

$myBody['name'] = "Demo";

$request = $client->put($url, ['body'=>$myBody]);

$response = $request->send();

dd($response);

}

DELETE Request:

public function deleteGuzzleRequest()

{

$client = new \GuzzleHttp\Client();

$url = "http://myexample.com/api/posts/1";

$request = $client->delete($url);

$response = $request->send();

dd($response);

}

As above example, you can see how it works.

I hope it can help you...




December 1, 2019 | Category : LaravelLaravel 6
Related Posts



Blog Search