DEV Community

Cover image for Multiple API in Laravel with HTTP Requests
Hòa Nguyễn Coder
Hòa Nguyễn Coder

Posted on

Multiple API in Laravel with HTTP Requests

Today, I share everyone , how to do call multiple api in Laravel
We now promise can call multiple api from client (vue, react, js,...). if you use Promise.all([...]), it return multiple promise

Okay, let't go

The first , you can install project Laravel 11

The second , we can setup it in web.php or controller,services,..

Example : I write code in web.php file, help test it

use Illuminate\Support\Facades\Http;
use GuzzleHttp\Promise\Utils;
use GuzzleHttp\Exception\ConnectException;
use Illuminate\Http\Client\Pool;

Route::get('multiple-request', function() {
    $responses = Utils::all([
        'foo' => Http::async()->get('https://jsonplaceholder.typicode.com/todos/1')->then(function ($response) {
            // you can check data, after then return it
            return $response;
        }),
        'bar' => Http::async()->get('https://jsonplaceholder.typicode.com/todos/2')->then(function ($response) {
             // you can check data, after then return it
            return $response;
        }),
        'baz' => Http::async()->get('https://jsonplaceholder.typicode.com/todos/3')->then(function ($response) {
            // you can check data, after then return it
            return $response;
        }),
    ])->wait();

    // you need check data here
    $fooOk = $responses['foo']->ok();
    $barSuccessful = $responses['bar']->successful();
    $connectionFailed = $responses['baz'] instanceof ConnectException;

    return [
        'foo' => $responses['foo']->ok() ? $responses['foo']->json() : 'Request to foo failed',
        'bar' => $responses['bar']->successful() ? $responses['bar']->json() : 'Request to bar failed',
        'baz' => $responses['baz']->ok() ? $responses['baz']->json() : 'Request to baz failed',
    ];
});

Enter fullscreen mode Exit fullscreen mode

When you request url : http://127.0.0.1/multiple-request , it return data

Multiple API in Laravel with HTTP Requests

Okay, or you can use Http::pool , request api

Route::get('multiple-request-using-pool', function() {

        $responses = Http::pool(fn (Pool $pool) => [
            $pool->get('https://jsonplaceholder.typicode.com/todos/1'),
            $pool->get('https://jsonplaceholder.typicode.com/todos/2'),
            $pool->get('https://jsonplaceholder.typicode.com/todos/3'),
        ]);

        $responses[0]->ok();
        $responses[1]->successful();
        // Kiểm tra và lấy nội dung từ các phản hồi
        $data = [
            'todo_1' => $responses[0]->ok() ? $responses[0]->json() : 'Request to todo 1 failed',
            'todo_2' => $responses[1]->successful() ? $responses[1]->json() : 'Request to todo 2 failed',
            'todo_3' => $responses[2]->ok() ? $responses[2]->json() : 'Request to todo 3 failed',
        ];

        return response()->json($data);

});

Enter fullscreen mode Exit fullscreen mode

Return data :

Multiple API in Laravel with HTTP Requests

The article : Multiple API in Laravel with HTTP Requests

Top comments (0)