Отправить одновременный HTTP-запрос с помощью библиотеки Guzzle - PullRequest
0 голосов
/ 04 марта 2019

Я хочу отправить запрос одновременно и получить данные.Вот мой текущий код:

 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; 

    }

Я хочу получить все постраничные данные конкретной страницы.Любая помощь будет очень ценной.

1 Ответ

0 голосов
/ 05 марта 2019

Для одновременного выполнения нескольких обещаний вам понадобятся следующие функции: 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;
}
...