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
ataşehir escort maltepe escort bostancı escort kadıköy escort maltepe escort ataşehir escort tuzla escort pendik escort ümraniye escort kartal escort
pendik escort
izmir escort izmir escort izmir escort izmir escort denizli escort antalya escort antalya escort antalya escort izmir escort izmir escort izmir escort izmir escort izmir escort
hatay escort nevsehir escort nigde escort kirikkale escort batman escort osmaniye escort kirklareli escort tokat escort rize escort adiyaman escort gumushane escort batman escort
porno izle
cialis satış cialis fiyat cialis 20 mg cialis 100 mg cialis 5 mg fiyat
sohbet okey oyna
ankara escort marmaris escort bursa escort mersin escort antalya escort izmir escort samsun escort bodrum escort çeşme escort kuşadası escort adana escort eskişehir escort gaziantep escort esenyurt escort pendik escort tuzla escort fatih escort ümraniye escort mecidiyeköy escort kartal escort bahçelievler escort bahçeşehir escort ataşehir escort bakırköy escort nişantaşı escort avcılar escort büyükçekmece escort kurtkoy escort konya escort beşiktaş escort taksim escort beylikdüzü escort malatya escort sakarya escort şişli escort maltepe escort kadıköy escort ataköy escort bostancı escort şirinevler escort bağcılar escort üsküdar escort etiler escort başaksehir escort ortaköy escort kağıthane escort sultangazi escort halkalı escort çekmeköy escort şaşkınbakkal escort mamak escort sincan escort keçiören escort çankaya escort beypazarı escort akyurt escort pursaklar escort kahramankazan escort göztepe escort alsancak escort bornova escort buca escort karabaglar escort karşıyaka escort konak escort narlıdere escort izmit escort başiskele escort kandira escort körfez escort karamürsel escort dilovası escort gölcük escort gebze escort kocaeli escort kayseri escort elazığ escort denizli escort diyarbakir escort adana escort ankara escort izmir escort antalya escort gaziantep escort kayseri escort konya escort mersin escort denizli escort kocaeli escort diyarbakır escort