我在循环中使用食肉来获取承诺。该字符串在循环中:
$rpomises[] = $this->client->getAsync($url, $options);
接下来,我做:
$res = collect(Promise\settle($promises)->wait());
如您所见,它只是一个带有字符串字段和GuzzleHttp\Rsr7\Response
对象的数组。那么如何从该构造中获取请求的URL?
谢谢您的帮助!
答案 0 :(得分:1)
我遇到了同样的问题。
我的用途:
use GuzzleHttp\Client;
use GuzzleHttp\Promise\EachPromise;
use GuzzleHttp\Psr7\Response;
use Requests;
从类中提取函数:
public function getUrlsInParallelRememberingSource($urls,
$numberThreads = 10)
{
$client = new Client();
$responsesModified = [];
$promises = (function () use ($urls, $client, &$responsesModified)
{
foreach ($urls as $url) {
yield $client->getAsync($url)->then(function($response) use ($url, &$responsesModified)
{
$data = [
'url' => $url,
'body' => 'res' // pass here whatever you want
];
$responsesModified[] = $data;
return $response;
});
}
})();
$eachPromise = new EachPromise($promises,
[
'concurrency' => $numberThreads,
'fulfilled' => function (Response $response)
{
},
'rejected' => function ($reason)
{
}
]);
$eachPromise->promise()->wait();
return $responsesModified;
}
这给出了以下结果:
http://i.kagda.ru/5001133750442_01-11-2020-00:34:55_5001.png