获取每个Guzzle请求端点的响应

时间:2020-03-27 08:27:31

标签: php guzzle

您如何从Guzzle中的一系列诺言中获得响应? 假设我有一个端点数组,使用getAsync

进行了调用
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Promise\EachPromise;
use GuzzleHttp\Promise as GuzzlePromise;


$url = ["4231?apikey=api&fields=bla","4232?apikey=api&fields=bla","4233?apikey=api&fields=bla"];

function getInfo($url){
   try {
        //each of which  should run like this right? 
        //https://api.com/api/v3/data/opportunities/4231?apikey=api&fields=bla
        $client = new Client(['base_uri' => 'https://api.com/api/v3/data/opportunities/']);
        //pushed all the endpoint here 
        $promises = [];
        foreach($url as $endpoint){
            $resp = $client->getAsync($endpoint);
            array_push($promises,$resp);
        };

         $responses = Promise\unwrap($promises);
         $responses = Promise\settle($promises)->wait();

         //I dont know which or is this the correct response.
         foreach($responses as $response){
            print_r($response);
        }
   }catch(MultiTransferException $e) {
         //handle error
    }
}

这就是我所得到的https://prnt.sc/rnnkgz 我想获得每个端点的每个响应json。

0 个答案:

没有答案
相关问题