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
Use the composer to install the guzzle package and run the following command:
composer require guzzlehttp/guzzle
get()
methodpublic function getGuzzleRequest()
{
$client = new \GuzzleHttp\Client();
$request = $client->get('http://example.com');// Url of your choosing
$response = $request->getBody();
dd($response);
}
In the code above, we perform a simple get()
request.
Here, we try to get data from a third party
In the code above, we used
example.com
. You will probably use aas it applies to your own application. URL Univeral Resource Language
Then execute a dd()
post()
methodpublic 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);
}
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 ofcurl
. But you can check out my shot on How to use curl in Laravel to figure out which one works for you.