Home > Software design >  Get first array from multiples endpoints async request
Get first array from multiples endpoints async request

Time:09-28

I have the guzzle configured which currently uses getAsync with 4 urls promise. The return of this data comes with 2 arrays, of this format below.

My question, how can I separate into two variables, the first being just the first array? In this example, it would be the entire array of sizes 3154 and in the other variable, the entire array of sizes 10297

Guzzle

$requests = [
            getenv('apiSavingNew'), 
            getenv('apiSavingOld'),
        ];

        $promises = (function () use ($requests) {
        $client = new Client([
            'verify' => false
        ]);

        foreach ($requests as $request) {
            yield $client->getAsync($request);      
        }
        })();

        $eachPromise = new EachPromise($promises, [
            'concurrency' => 2,
            'fulfilled' => function (Response $response) {
            if ($response->getStatusCode() == 200) {
                $request = json_decode($response->getBody());
                    $firstRequest = // first array here
                    $secondRequest = // second array here
                }
            },
            'rejected' => function (RequestException $e) {
                echo $e->getMessage();
            }
        ]);

        $eachPromise->promise()->wait();

Return guzzle promise

array (size=3154)
  0 => 
    object(stdClass)[11532]
      public 'id' => string '57a64bb0-1c6a-11ec-bfd3-173b9227de8c' (length=36)
      public 'createdAt' => string '2021-09-23T12:32:40.427Z' (length=24)
      public 'data' => string '2021-09-22T00:00:00.000Z' (length=24)
      public 'dataFim' => string '2021-10-22T00:00:00.000Z' (length=24)
      public 'valor' => string '0.30120' (length=7)
      public 'serieTemporalId' => string 'a43978f1-7fd4-4550-9907-106474e64ee4' (length=36)
      public 'acumuladoAno' => null
      public 'acumulado12Meses' => null
  1 => 
    object(stdClass)[11539]
      public 'id' => string '57a49e00-1c6a-11ec-bfd3-173b9227de8c' (length=36)
      public 'createdAt' => string '2021-09-23T12:32:40.416Z' (length=24)
      public 'data' => string '2021-09-21T00:00:00.000Z' (length=24)
      public 'dataFim' => string '2021-10-21T00:00:00.000Z' (length=24)
      public 'valor' => string '0.30120' (length=7)
      public 'serieTemporalId' => string 'a43978f1-7fd4-4550-9907-106474e64ee4' (length=36)
      public 'acumuladoAno' => null
      public 'acumulado12Meses' => null
   more elements...

array (size=10297)
  0 => 
    object(stdClass)[11545]
      public 'id' => string '54f70a30-1c6a-11ec-bfd3-173b9227de8c' (length=36)
      public 'createdAt' => string '2021-09-23T12:32:35.923Z' (length=24)
      public 'data' => string '2021-09-22T00:00:00.000Z' (length=24)
      public 'dataFim' => string '2021-10-22T00:00:00.000Z' (length=24)
      public 'valor' => string '0.50000' (length=7)
      public 'serieTemporalId' => string 'ec940ca2-7da8-4a75-ae7b-d90244797b65' (length=36)
      public 'acumuladoAno' => null
      public 'acumulado12Meses' => null
  1 => 
    object(stdClass)[11557]
      public 'id' => string '54f3fcf0-1c6a-11ec-bfd3-173b9227de8c' (length=36)
      public 'createdAt' => string '2021-09-23T12:32:35.903Z' (length=24)
      public 'data' => string '2021-09-21T00:00:00.000Z' (length=24)
      public 'dataFim' => string '2021-10-21T00:00:00.000Z' (length=24)
      public 'valor' => string '0.50000' (length=7)
      public 'serieTemporalId' => string 'ec940ca2-7da8-4a75-ae7b-d90244797b65' (length=36)
      public 'acumuladoAno' => null
      public 'acumulado12Meses' => null
   more elements...

CodePudding user response:

I didnot understood completely what your requirement is? but still I hope that this might help. You can add the second argument to function in fulfilled as $index and add it in a separate variable for $results empty array.

$results = [];
$requests = [
    getenv('apiSavingNew'), 
    getenv('apiSavingOld'),
];

$promises = (function () use ($requests) {
$client = new Client([
    'verify' => false
]);

foreach ($requests as $request) {
    yield $client->getAsync($request);      
}
})();

$eachPromise = new EachPromise($promises, [
    'concurrency' => 2,
    'fulfilled' => function (Response $response, $index) {
    if ($response->getStatusCode() == 200) {
        //$request = json_decode($response->getBody());
        $results[$index] = json_decode($response->getBody(), true);
        }
    },
    'rejected' => function (RequestException $e, $index) {
        echo $e->getMessage();
    }
]);

$eachPromise->promise()->wait();
  • Related