如何在Guzzle6中创建批量请求?

时间:2015-06-24 06:59:02

标签: php guzzle guzzle6

我需要发送多个请求,因此我想实现批量请求。

我们怎样才能在Guzzle6中做到这一点?

使用旧方式:

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

给我错误:

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

1 个答案:

答案 0 :(得分:11)

尝试这样的事情

    $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
    }

别忘了

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