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

Rah*_*ulG 9 php guzzle guzzle6

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

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

使用旧方式:

$client->send(array(
    $client->get($courses),  //api url
    $client->get($job_categories), //api url
)); 
Run Code Online (Sandbox Code Playgroud)

给我错误:

GuzzleHttp\Client::send() must implement interface Psr\Http\Message\RequestInterface, array given
Run Code Online (Sandbox Code Playgroud)

小智 16

尝试这样的事情

    $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
    }
Run Code Online (Sandbox Code Playgroud)

别忘了

use GuzzleHttp\Pool;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
Run Code Online (Sandbox Code Playgroud)