Laravel PHP guzzle http client GET and POST request example

Laravel PHP guzzle http client GET and POST request example

In this tutorial, I will let you know the use of Guzzle HTTP client and how to send request to server from Laravel application using Guzzle HTTP client and get the HTTP response.

It's very easy to send an HTTP request using Guzzle with a simple interface that you do with the cURL.

You can use Guzzle to make authenticable request.

In this example, I will show you how to get the user details registered with "github" by their credentials.


First, I will have to install the Guzzle through Composer by running following command :

composer require guzzlehttp/guzzle:~6.0

Now you can use the GuzzleHttp\Client class in your PHP application to make a request on server.


Sending GET request is very common form of HTTP requests. Normally you open the any website in the browser then the HTML pages of the website is downloaded by HTTP GET request.

Example 1 :

  1. $client = new \GuzzleHttp\Client();
  3. // Create a request
  4. $request = $client->get('');
  5. // Get the actual response without headers
  6. $response = $request->getBody();
  7. return $response;
Example 2 :

  1. $client = new \GuzzleHttp\Client();
  2. // Create a request with auth credentials
  3. $request = $client->get('',['auth'=>['username','password']]);
  4. // Get the actual response without headers
  5. $response = $request->getBody();
  6. return $response;

POST request is basically used to submit form data to a website, There can be number of reasons to use POST request.

  1. $client = new \GuzzleHttp\Client();
  2. $body['name'] = "Testing";
  3. $url = "";
  4. $response = $client->createRequest("POST", $url, ['body'=>$body]);
  5. $response = $client->send($response);
  6. return $response;

You can also send the PUT/DELETE/PATCH request in following way :

GET:	   $client->get('', [/** options **/])
POST:	   $client->post('', [/** options **/])
HEAD:	   $client->head('', [/** options **/])
PUT:	   $client->put('', [/** options **/])
DELETE:	   $client->delete('', [/** options **/])
OPTIONS:   $client->options('', [/** options **/])
PATCH:	   $client->patch('', [/** options **/])

Phone: (+91) 8800417876
Noida, 201301