How To Use Laravel’s Built-In Client to Interact with External APIs

1. Install the Guzzle HTTP Client:

The first step is to install the Guzzle HTTP Client. This is a PHP HTTP client that allows you to make HTTP requests from your Laravel application. To install it, run the following command in your terminal:

composer require guzzlehttp/guzzle

2. Create a Client Instance:

Once you have installed the Guzzle HTTP Client, you can create a client instance. This is done by creating a new instance of the GuzzleHttp\Client class. You can do this in your controller or service class:

$client = new GuzzleHttp\Client();

3. Make a Request:

Once you have created a client instance, you can make a request to an external API. To do this, you can use the request() method of the client instance. This method takes two arguments: the HTTP method and the URL of the API endpoint. For example, to make a GET request to an API endpoint, you can use the following code:

$response = $client->request(‘GET’, ‘https://example.com/api/endpoint’);

4. Handle the Response:

Once you have made the request, you can handle the response. The response is returned as an instance of the GuzzleHttp\Psr7\Response class. You can use the getBody() method of this class to get the response body as a string. You can then use the json_decode() function to convert the response body into a PHP array:

$responseBody = $response->getBody();
$data = json_decode($responseBody, true);

Laravel makes API interactions a breeze for both new and experienced web developers. The Larvel HTTP client is built on top of PHP’s Guzzle HTTP client to give developers a smoother experience when making HTTP requests. Its primary features include authentication, routing, and effective object-relational mapping (ORM).

This article will explore using Laravel’s HTTP client to make requests, debug responses, create middleware and macros, and more.

Laravel HTTP Client Does the Hard Work for You for APIs

Guzzle is a simple HTTP client for PHP. It offers functionality for different form requests, including GET, POST, PUT, and DELETE alongside the streaming capabilities and multipart requests. With the Guzzle HTTP client, sending synchronous and asynchronous requests to the server is possible. Furthermore, it also comes with decent middleware to customize the client’s behavior.

Laravel’s HTTP client is a wrapper built on Guzzle but with extra functionalities. It includes support for retrying failed requests and some helper functions with JSON data. Most of the functionalities of Laravel HTTP clients are similar to Guzzle.

Prerequisites

In the following sections, you’ll learn more about Laravel’s HTTP client. To follow along, you will need:

  • Basic knowledge of Laravel, PHP, and APIs
  • PHP and Composer installed
  • Postman

How To Make Requests

To understand how to use an HTTP client to make a request, you can leverage plenty of hosted APIs, such as ReqRes.

Start by importing the HTTP package included when creating the application. Inside the App/Http/Controllers/UserController.php file, add the following code, starting with the use statement at the beginning of the file and the remaining code inside the index function.

use Illuminate\Support\Facades\Http;
return Http::get("https://reqres.in/api/users?page=2");

Note: For complex use cases, you can also send the request with headers by using the withHeaders method.

In the same file, create a new method post using the code below:

function post()
{
    $response = Http::withHeaders([
        'Content-Type' => 'application/json',
    ])->post('https://reqres.in/api/users', [
        'name' => 'morpheus',
        'job' => 'leader',
    ]);
    return $response;
}

Then add a route for it inside the routes/web.php file:

Route::get('post',[UserController::class,'post']);

Now, Postman can be used to test this route. Open Postman and add http://127.0.0.1:8000/post as the URL, with the type of request as GET. Once you click send, you’ll see the following response:

Making requests using Postman
Making requests using Postman

Concurrent Requests

Parallel requests significantly improve performance as you can fetch more data in the same period. Laravel’s HTTP client makes it possible to carry out concurrent requests using the pool method.

Inside App/Http/Controllers/UserController.php, add the following code:

use Illuminate\Http\Client\Pool;
function concurrent()
{
    $responses = Http::pool(fn (Pool $pool) => [
        $pool->get('https://reqres.in/api/users?page=2'),
        $pool->get('https://reqres.in/api/users/2'),
        $pool->get('https://reqres.in/api/users?page=2'),
    ]);

    return $responses[0]->ok() &&
        $responses[1]->ok() &&
        $responses[2]->ok();
}

Then, add the supporting route inside the routes/web.php file.

Route::get('concurrent',[UserController::class,'concurrent']);

The browser gives the following response when the route is visited:

Concurrent requests
Concurrent requests

Request Macros

Request macros are useful when interacting with common API paths.

To create the macro, you need to define the macro inside the boot method of the app/Http/Providers/AppServiceProvider.php file using the code below:

use Illuminate\Support\Facades\Http;
Http::macro('reqres', function () {
    return Http::baseUrl('https://reqres.in/api');
});

Note: Make sure to add the use statement at the beginning of the file.

Then, use the macro inside the UserController by adding the following code:

function macro()
{
    $response = Http::reqres()->get('/users?page=2');
    return $response;
}

As you can see, because the macro is already being created, you don’t have to add the full URL again.

Lastly, add a route in the routes/web.php file using the code below:

Route::get('macro',[UserController::class,'macro']);
Macro request
Macro request

How To Decode Responses

To decode a response and ensure that an API request is successful, you use the status method included in the client. This method gets the status code sent from the server and displays it.

To test this out, replace the previous macro code with the code below inside the App/Http/Controllers/UserController.php file:

function macro()
{
    $response = Http::reqres()->get('/users?page=2');
    return $response->status();
}

Here, the status code 200 means the request was successful.

Successful decoding response
Successful decoding response

How To Test JSON APIs

Laravel has several helpers to test the JSON APIs and their responses. The helper functions include json, getJson, postJson, putJson, patchJson, deleteJson, and so on.

To understand the Testing better, create a test scenario for the GET user’s route. When you bootstrap the Laravel application, the Example Test is already created. Inside the tests/Feature/ExampleTest.php file, replace the existing code with the following:

<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function test_example()
    {
        $response = $this->getJson('/users');

        $response->assertStatus(200);
    }
}

The added code fetches the JSON data at the user’s route and checks if the status code is 200 or not.

Once you’ve added the test code, run the following command in your terminal to run the tests:

./vendor/bin/phpunit

Once the tests are completed, you’ll see that it ran two tests, both of which were successful.

Testing JSON APIs
Testing JSON APIs

Similarly, you can check for different types of requests and utilize other helper methods for more sophisticated testing.

How To Handle Events

Laravel offers three events to be fired when dealing with HTTP requests.

  • RequestSending, which is before the request is sent.
  • ResponseReceived, which is when a response is received.
  • ConnectionFailed, which is when no response is received.

All three events include the $request property to inspect the Illuminate\Http\Client\Request instance, and ResponseReceived has an additional $response property. These are particularly useful for performing actions after an event. For example, you might want to email after getting a successful response.

To create an event and listener, navigate to the app/Providers/EventServiceProvider.php file and replace the listen array with the following code.

protected $listen = [
    Registered::class => [
        SendEmailVerificationNotification::class,
    ],
    'Illuminate\Http\Client\Events\ResponseReceived' => [
        'App\Listeners\LogResponseReceived',
    ],
];

Then run the following command in your terminal:

php artisan event:generate

The above command will create the app/Listeners/LogResponseReceived.php listener. Replace the code of that file with the code below:

<?php
namespace App\Listeners;
use Illuminate\Http\Client\Events\ResponseReceived;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Log;
class LogResponseReceived
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct(Request $request, Response $response)
    {
        Log::channel('stderr')->info($response->status());
    }

    /**
     * Handle the event.
     *
     * @param  \Illuminate\Http\Client\Events\ResponseReceived  $event
     * @return void
     */
    public function handle(ResponseReceived $event)
    {
        
    }
}

The info log of the status code is printed in the terminal.

Terminal logs showing the status code
Terminal logs showing the status code

 

Summary

Whether a website or web application is made by an organization or an independent developer, APIs are key to their success. However, using them can be difficult.

Many frameworks and libraries promise to simplify this process, but Laravel stands out for its focus on simplicity and ease of use. Their built-in client supports easy API calls, concurrent API calls, API Macros, helper methods for JSON-based APIs, and more.


Get all your applications, databases and WordPress sites online and under one roof. Our feature-packed, high-performance cloud platform includes:

  • Easy setup and management in the MyKinsta dashboard
  • 24/7 expert support
  • The best Google Cloud Platform hardware and network, powered by Kubernetes for maximum scalability
  • An enterprise-level Cloudflare integration for speed and security
  • Global audience reach with up to 35 data centers and 275 PoPs worldwide

Get started with a free trial of our Application Hosting or Database Hosting. Explore our plans or talk to sales to find your best fit.





Source link

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?