我希望在一个循环中执行某些操作,然后在请求结束时继续执行脚本,例如我想做这样的事情:
$client = new \GuzzleHttp\Client();
$promise = $client->headAsync("https://www.google.com");
$promise->then(
function (ResponseInterface $res) {
echo $res->getStatusCode() . "\n";
},
function (RequestException $e) {
echo $e->getMessage() . "\n";
}
);
while($promise->getState() === "pending"){
$queue = \GuzzleHttp\Promise\queue();
$queue->run();
echo "Waiting\n";
sleep(1);
}
这段代码只打印“等待”。我怎么能实现这个目标呢?
答案 0 :(得分:4)
正在寻找相同的答案,所以这是我发现的,你需要tick
curl请求(以便请求实际上是进程)。
工作示例:
$curl = new \GuzzleHttp\Handler\CurlMultiHandler();
$handler = \GuzzleHttp\HandlerStack::create($curl);
$client = new \GuzzleHttp\Client(['handler' => $handler]);
$promise = $client->headAsync("https://www.google.com");
$promise->then(
function (ResponseInterface $res) {
echo $res->getStatusCode() . "\n";
},
function (RequestException $e) {
echo $e->getMessage() . "\n";
}
);
$queue = \GuzzleHttp\Promise\queue();
while($promise->getState() === "pending"){
$curl->tick();
//echo "Waiting\n"; Commented out as it prints A LOT without sleep, but sleep slows down the rest.
}
在查看此问题后,我将此代码放在一起:https://github.com/guzzle/guzzle/issues/1127