11

我需要发送多个请求,所以我想实现一个批处理请求。

我们如何在 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
4

1 回答 1

22

尝试这样的事情

    $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;
于 2016-07-13T12:24:49.510 回答