-
-
Save kurozumi/605d5ccd45f1b3216db7 to your computer and use it in GitHub Desktop.
Guzzle6でマルチリクエスト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'vendor/autoload.php'; | |
use GuzzleHttp\Pool; | |
use GuzzleHttp\Clinet; | |
use GuzzleHtp\Psr7\Request; | |
$client = new Client(); | |
$requests[] = new Request('GET', 'http://sample1.com'); | |
$requests[] = new Request('GET', 'http://sample2.com'); | |
$requests[] = new Request('GET', 'http://sample3.com'); | |
$requests[] = new Request('GET', 'http://sample4.com'); | |
$requests[] = new Request('GET', 'http://sample5.com'); | |
$pool = new Pool($client, $requests,[ | |
'concurrency' => 5, // 同時接続数 | |
'fulfilled' => function($response, $index){ // 成功したレスポンスはこちらへ | |
if($response->getStatusCode() == 200) | |
echo $response->getBody()->getContents(); | |
}, | |
'rejected' => function($reason, $index){}, // 失敗したレスポンスはこちらへ | |
]); | |
// 転送を開始し、プロミスを作成 | |
$promise = $pool->promise(); | |
// リクエスト実行 | |
$promise->wait(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment