使用 guzzle 库同时发送 HTTP 请求

Send Simultaneous HTTP request using guzzle library

我想同时发送请求和获取数据。这是我当前的代码:

 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; 

    }

我想获取特定页面的所有分页数据。任何帮助将不胜感激。

要同时执行一堆 promise,您需要以下函数: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;
}