How to make create batch request in Guzzle6?
Asked Answered
F

1

11

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
Fradin answered 24/6, 2015 at 6:59 Comment(0)
R
22

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;
Roccoroch answered 13/7, 2016 at 12:24 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.