Making Better HTTP Requests In Laravel 7 With Http Methods

Laravel relies heavily on Guzzle (a PHP HTTP client that makes it easy to make HTTP requests) to make HTTP requests a breeze for developers. However, earlier versions (5.*, I never used Laravel 6) of Laravel did not have convenient methods for interacting with Guzzle as we do with Laravel 7.
Laravel 7 makes it a breeze to make outgoing HTTP requests to communicate with other web applications. The methods introduced in Laravel 7 will save you time and some lines of code making your code prettier. To start using Guzzle:
First, install the package as a dependency of your application
composer require guzzlehttp/guzzle
Performing a Get Request
Once composer has completed, you can dive straight in and start making requests to the API you wish to interact with.
use Illuminate\Support\Facades\Http;
$response = Http::get(‘https://whatever-webservice.com');
Check if Response is Successful
You can check if your request was successful and returned a 200 using the successful() method.
$response->successful()
The successful method returns a Boolean which you therefore use to enact conditions and further evaluate your code. Note that for some web services it is better to check for the success message returned in the response body rather than using the success method.
$response->successful() ? ‘Do this’ : ‘Do that’;
Getting Response Status
You can also get the status of the response returned using the status() method. You can then go ahead to evaluate your code based on the nature of response you have received.
$response->status() === 200 ? ‘Do this’ : ‘Do that’;
Get Your Response as an Array
You can convert your response to an array without using the json_decode(), instead use the json() method and you will get an array return.
$response = Http::get(‘https://whatever-webservice.com');
$response->json();
Directly Access Array Data
This comes in handy when you need to pull off limited data from the response.
return Http::get(‘http://whatever-webservice.com/user/1')['name'];
This will return the name of the user.
Making a Post Request
Post requests are by default sent as application/json. A simple post request would look like this:
$response= Http::post(‘https://whatever-webservice.com', [
‘name’ => ‘Elvis Onobo’
]);
Adding Headers with Your Request
You may add headers with your request using the withHeaders() method.
Http::withHeaders([‘Cache-Control’=>’no-cache’])
->post(‘ https://whatever-webservice.com', [‘name’=>’Elvis Onobo’]);
Bearer Tokens
There’s almost always a need to get authenticated by the web-service you may be interacting with. You can quickly add an Authorization Bearer token to the header request. Laravel really simplifies this.
$response= Http::withToken(‘your-token’)->post([‘name’ => ‘Elvis Onobo’]);
$response= Http::withToken(‘your-token’)->get(‘https://whatever-webservice.com');
Retries
Lastly, Laravel gives a handy method that makes sure your API call is not abandoned just after the first try. The retry method will make the call again until the number of retries are satisfied.
This is useful when one API call depends on another. For example, after a transaction you want to verify for success from the client before saving to your database.
$response= Http::retry(1, 1000)->get(‘https://whatever-webservice.com');
The first parameter the retry method takes (1) is the number of times you want to retry and the second parameter is the number of milliseconds to wait before trying again.