当前位置: 首页 > 工具软件 > Guzzle-Swoole > 使用案例 >

PHP guzzle异步请求数据,如何在不阻塞的情况下处理GuzzleHTTP异步请求?

宫俊远
2023-12-01

$handler = new \GuzzleHttp\Handler\CurlMultiHandler();

$client = new \GuzzleHttp\Client(['handler' => $handler]);

$promise1 = $client->getAsync("http://www.stackoverflow.com");

$promise2 = $client->getAsync("http://localhost/");

$doneCount = 0;

$promise1->then(function() use(&$doneCount) {

$doneCount++;

echo 'Promise 1 done!';

});

$promise2->then(function() use(&$doneCount) {

$doneCount++;

echo 'Promise 2 done!';

});

$last = microtime(true);

while ( $doneCount < 2 ) {

$now = microtime(true);

$delta = round(($now-$last)*1000);

echo "tick($delta) ";

$last = $now;

$handler->tick();

}

CurlMultiHandler

tick()

$handler = new \GuzzleHttp\Handler\CurlMultiHandler(['select_timeout' => 0.5]);

 类似资料: