方法その1
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
$client = new Client([
'base_uri' = 'https://sample.com/',
]);
// /test1,/test2,/test3に非同期でgetを投げる
$promises[] = $client->getAsync('/test1');
$promises[] = $client->getAsync('/test2');
$promises[] = $client->getAsync('/test3');
//それぞれのリクエストの終了を待つ
$results = Promise\unwrap($promises);
方法その2(不特定数のリクエストを送るときに使う)
use GuzzleHttp\Pool;
use GuzzleHttp\Promise;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$client = new Client([
'base_uri' = 'https://sample.com/'
]);
// /test1,/test2,/test3, ...へのリクエストを生成するジェネレータ
$requests = function($total){
for($i = 1; $i < $total + 1; $i++){
yield new Request('GET', '/test' + $i);
}
};
// 100個のリクエストを設定
$pool = new Pool($client, $requests(100), [
'concurrency' => 5, //並列数
'fulfilled' => function($response, $index){
// 成功したリクエストのコールバック処理
},
'rejected' => function($reason, $index){
// 失敗したリクエストのコールバック処理
},
]);
// リクエストを投げる
$promise = $pool->promise();
// 全てのリクエストの終了を待つ
$promise->wait();


