使用guzzle库发送同步HTTP请求

问题描述 投票:0回答:1

我想发送同步请求并获取数据。这是我目前的代码:

 public function getDispenceryforAllPage($dispencery)
    {
        $data = array();
        $promiseGetPagination = $this->client->getAsync($dispencery)
            ->then(function ($response) {
                return $this->getPaginationNumber($response->getBody()->getContents());           
                });
               $Pagination = $promiseGetPagination->wait();


                for ($i=1; $i<=$Pagination; $i++) {

                        $GetAllproducts = $this->client->getAsync($dispencery.'?page='.$i)
                        ->then(function ($response) {

                            $promise =  $this->getData($response->getBody()->getContents()); 
                            return $promise;       
                            });
                            $data[] = $GetAllproducts->wait();  

        }
        return $data; 

    }

我想获取特定页面的所有分页数据。任何帮助都会非常值得赞赏。

php asynchronous guzzle synchronous
1个回答
0
投票

要同时执行一堆promises,你需要函数:all(),some(),each()和guzzlehttp / promises包中的其他函数。

试试这个:

public function getDispenceryforAllPage($dispencery)
{
    $Pagination = $this->getPaginationNumber(
        $this->client->get($dispencery)->getBody()->getContents()
    );

    $GetAllProductPromises = array();
    for ($i = 1; $i <= $Pagination; $i++) {
        $GetAllProductPromises[] = $this->client->getAsync($dispencery . '?page=' . $i)
            ->then(function ($response) {
                return $this->getData($response->getBody()->getContents());
            });
    }

    $data = \GuzzleHttp\Promise\all($GetAllProductPromises);

    return $data;
}
© www.soinside.com 2019 - 2024. All rights reserved.