What role may Guzzle HTTP Client play in your PHP Laravel GET and POST requests? We will attempt to respond to that in this tutorial. If you’ve been searching the internet for a Laravel Guzzle http client example, your search is over.
If I think back to the early days of web development, we relied on cURL to perform tasks like this. However, a lot of advancements were made over time. Guzzle is one of such developments, and now I’d like to talk about the Guzzle HTTP client.
We’ll look at how to build the Guzzle HTTP Client in Laravel and learn how to submit HTTP requests in this tutorial.
Install Guzzle Http Package
Guzzle is a PHP HTTP client that makes it simple to interface with online services and submit HTTP queries. It offers a user-friendly interface that is both straightforward and effective for making POST requests, streaming huge uploads and downloads, using HTTP cookies, uploading JSON data, etc.
Ideally, we need to install the guzzlehttp/guzzle package using Composer package manager in order to send HTTP queries.
composer require guzzlehttp/guzzle
Guzzle 6’s best feature, and what draws my attention to it, is the ability to simultaneously submit synchronous and asynchronous queries from the same interface. Additionally, this provides countless settings for use with http requests.
Guzzle HTTP Client CRUD Requests
You may get a general concept about making GET, POST, PUT, and DELETE requests with Guzzle 6 HTTP Client library in Laravel from the information provided below.
Guzzle GET Request
public function guzzleGet()
{
$client = new \GuzzleHttp\Client();
$request = $client->get('http://testmyapi.com');
$response = $request->getBody();
dd($response);
}
Guzzle POST Request
public function guzzlePost()
{
$client = new \GuzzleHttp\Client();
$url = "http://testmyapi.com/api/blog";
$myBody['name'] = "Demo";
$request = $client->post($url, ['body'=>$myBody]);
$response = $request->send();
dd($response);
}
Guzzle PUT Request
public function guzzlePut()
{
$client = new \GuzzleHttp\Client();
$url = "http://testmyapi.com/api/blog/1";
$myBody['name'] = "Demo";
$request = $client->put($url, ['body'=>$myBody]);
$response = $request->send();
dd($response);
}
Guzzle DELETE Request
public function guzzleDelete()
{
$client = new \GuzzleHttp\Client();
$url = "http://testmyapi.com/api/blog/1";
$request = $client->delete($url);
$response = $request->send();
dd($response);
}