Laravel PHP guzzle http client GET and POST request example

Laravel PHP guzzle http client GET and POST request example

In this post we will give you information about Laravel PHP guzzle http client GET and POST request example. Hear we will give you detail about Laravel PHP guzzle http client GET and POST request exampleAnd how to use it also give you demo for it if it is necessary.

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 GuzzleHttpClient 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=newGuzzleHttpClient();
  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;
$client = new GuzzleHttpClient();
		
// Create a request
$request = $client->get('example.com');

// Get the actual response without headers
$response = $request->getBody();

return $response;


Example 2 :

  1. $client=newGuzzleHttpClient();
  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;
$client = new GuzzleHttpClient();
// Create a request with auth credentials
$request = $client->get('https://api.github.com/user',['auth'=>['username','password']]);

// Get the actual response without headers
$response = $request->getBody();

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=newGuzzleHttpClient();
  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;
$client = new GuzzleHttpClient();

$body['name'] = "Testing";

$url = "http://my-domain.com/api/v1/post";

$response = $client->createRequest("POST", $url, ['body'=>$body]);

$response = $client->send($response);

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 **/])


You can use Guzzle in your application where you will need to work on the web services.

Hope this code and post will helped you for implement Laravel PHP guzzle http client GET and POST request example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  33  =  39

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US