如何获取大量异步请求的请求网址?

时间:2019-11-27 10:46:05

标签: php guzzle

我在循环中使用食肉来获取承诺。该字符串在循环中:

$rpomises[] = $this->client->getAsync($url, $options);

接下来,我做:

$res = collect(Promise\settle($promises)->wait());

结果中的一项是: enter image description here

如您所见,它只是一个带有字符串字段和GuzzleHttp\Rsr7\Response对象的数组。那么如何从该构造中获取请求的URL?

谢谢您的帮助!

1 个答案:

答案 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;
}

这给出了以下结果:

enter image description here

http://i.kagda.ru/5001133750442_01-11-2020-00:34:55_5001.png