【问题标题】:add more requests to the guzzle request pool before it exits the iteration在退出迭代之前向 guzzle 请求池添加更多请求
【发布时间】:2021-07-23 23:53:52
【问题描述】:

有没有办法在退出循环之前向 guzzle 7 请求队列添加另一个异步请求(可能在另一个请求的承诺之后)?

https://docs.guzzlephp.org/en/stable/quickstart.html#async-requests

【问题讨论】:

  • 如果您对找到的内容感到满意并且对您有用,您应该在答案块中提供它(而不是将其写为问题)该问题仍然活跃 3 天,因为没有答案已添加

标签: php asynchronous guzzle


【解决方案1】:
protected function download(string $url)
{
    $fulfilled = function (\GuzzleHttp\Psr7\Response $response) {
        // response is ready
        (string)$response->getBody();
        if('some other url ?'){
            $this->download('https:...........');
        }
    };
    
    $this->iterator->append($this->client->getAsync($url)->then($fulfilled));
}



private function work()
{
    $this->client = new \GuzzleHttp\Client();
    $this->iterator = new \ArrayIterator();

    $this->download('https:...........');

    $a = \GuzzleHttp\Promise\Utils::unwrap($this->iterator);
}

【讨论】:

    猜你喜欢
    • 2015-03-22
    • 2017-07-09
    • 2018-09-03
    • 2015-04-14
    • 2017-02-10
    • 2017-01-20
    • 2019-10-30
    • 1970-01-01
    • 2017-09-14
    相关资源
    最近更新 更多