How to make create batch request in Guzzle6?

4.2k Views Asked by At

I need to send multiple requests so I want to implement a batch request.

How can we do it in Guzzle6?

Using the the old way:

$client->send(array(
    $client->get($courses),  //api url
    $client->get($job_categories), //api url
)); 

is giving me the error:

GuzzleHttp\Client::send() must implement interface Psr\Http\Message\RequestInterface, array given
1

There are 1 best solutions below

1
On BEST ANSWER

try something like this

    $client = new Client();
    foreach ($links as $link) {
        $requests[] = new Request('GET', $link);
    }

    $responses = Pool::batch($client, $requests, array(
        'concurrency' => 15,
    ));

    foreach ($responses as $response) {
         //do something
    }

don't forget

use GuzzleHttp\Pool;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;