Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

laravel
php
communitycreator

How to use Guzzle HTTP client request in Laravel

Chinweuba Elijah Azubuike

A Guzzle is a PHP HTTP CLIENT that we use to send HTTP requests for trivial integration with web services, such as:

  • PATCH
  • PUT
  • GET
  • DELETE
  • POSTS

If you’ve used the bulk SMS services, you may have come across a Guzzle. In this shot, you will learn how to use Guzzle to send the HTTPHyperText Transfer Protocol request.

How to use Guzzle

Step 1: Install Guzzle package

Use the composer to install the guzzle package and run the following command:

composer require guzzlehttp/guzzle

Step 2: Do a demo request

The get() method

public function getGuzzleRequest()
{
    $client = new \GuzzleHttp\Client();
    $request = $client->get('http://example.com');// Url of your choosing
    $response = $request->getBody();
   
    dd($response);
}

Explanation

In the code above, we perform a simple get() request.

Here, we try to get data from a third party URIUniversal Resource Identifier.

In the code above, we used example.com. You will probably use a URLUniveral Resource Language as it applies to your own application.

Then execute a dd()dump and die response.

The post() method


public function postGuzzleRequest()
{
    $client = new \GuzzleHttp\Client();
    $url = "http://example.com/api/posts";
   
    $data['name'] = "LaravelCode";
    $request = $client->post($url,  ['body'=>$data]);
    $response = $request->send();
  
    dd($response);
}

Explanation

In the code above, I send data to http://example.com/api/posts. I also dump and die the response dd($response).


I prefer using guzzle in place of curl. But you can check out my shot on How to use curl in Laravel to figure out which one works for you.

RELATED TAGS

laravel
php
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike
RELATED COURSES

View all Courses

Keep Exploring