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.

Installation

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.

GET

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();
  2.         
  3. // Create a request
  4. $request = $client->get('example.com');
  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('https://api.github.com/user',['auth'=>['username','password']]);
  4. // Get the actual response without headers
  5. $response = $request->getBody();
  6. return $response;
POST

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 = "http://my-domain.com/api/v1/post";
  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('http://my-domain.com/get', [/** options **/])
POST:	   $client->post('http://my-domain.com/post', [/** options **/])
HEAD:	   $client->head('http://my-domain.com/get', [/** options **/])
PUT:	   $client->put('http://my-domain.com/put', [/** options **/])
DELETE:	   $client->delete('http://my-domain.com/delete', [/** options **/])
OPTIONS:   $client->options('http://my-domain.com/get', [/** options **/])
PATCH:	   $client->patch('http://my-domain.com/put', [/** options **/])

Phone: (+91) 8800417876
Noida, 201301
Attention Required! | Cloudflare

Sorry, you have been blocked

You are unable to access ressim.net

Why have I been blocked?

This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.

What can I do to resolve this?

You can email the site owner to let them know you were blocked. Please include what you were doing when this page came up and the Cloudflare Ray ID found at the bottom of this page.