Home > Enterprise >  How to handle JSON data from multiple responses in PHP?
How to handle JSON data from multiple responses in PHP?

Time:09-16

I am using curl_multi to send 2 post requests, the data is coming back as one $response correctly, I need to know the correct way to now handle this JSON data in PHP.

When I echo between <pre> tags my JSON is displayed correctly, however now I'm not sure how to take the data I want from it with PHP.

This is usually an easy task with a single API but I'm not sure why I'm having so much trouble with this here whilst using curl_multi!

json being returned inside my pre tags;

{
  "Vehicles": [
    {
      "ExternalVehicleId": "9uq0jz1c",
      "StockId": "1234",
      "Errors": []
    }
  ]
}

{
  "Finance": [
    {
      "ExternalVehicleId": "9uq0jz1d",
      "StockId": "4321",
      "Errors": []
    }
  ]
}

This is how the json comes back, but there are way more nested arrays. All appear to be in correct syntax wise.

And here's my php which I'm pretty sure is all fine;

$mh = curl_multi_init();
            foreach ($urls as $key => $url) {
                $chs[$key] = curl_init($url);
                curl_setopt($chs[$key], CURLOPT_RETURNTRANSFER, true);
                curl_setopt($chs[$key], CURLOPT_HEADER, false);
                curl_setopt($chs[$key], CURLOPT_CONNECTTIMEOUT, 200);
                curl_setopt($chs[$key], CURLOPT_TIMEOUT_MS, 25000);
                curl_setopt($chs[$key], CURLOPT_CUSTOMREQUEST, "POST");
                curl_setopt($chs[$key], CURLOPT_POST, true);
                curl_setopt($chs[$key], CURLOPT_POSTFIELDS, json_encode($request_contents[$key]));
                curl_setopt($chs[$key], CURLOPT_HTTPHEADER, $headers);

                curl_multi_add_handle($mh, $chs[$key]);
            }

     
            $running = null;
            do {
                curl_multi_exec($mh, $running);
            } while ($running);

            
            foreach (array_keys($chs) as $key) {
                $error = curl_error($chs[$key]);
                $last_effective_URL = curl_getinfo($chs[$key], CURLINFO_EFFECTIVE_URL);
                $time = curl_getinfo($chs[$key], CURLINFO_TOTAL_TIME);
                $response = curl_multi_getcontent($chs[$key]);  // get results
                if (!empty($error)) {
                    echo "The request $key return a error: $error" . "\n";
                } else {
                    echo "The request to '$last_effective_URL' returned '$response' in $time seconds." . "\n";
                    echo "<pre>";
                    echo $response;
                    echo "</pre>";
                    
                }

                curl_multi_remove_handle($mh, $chs[$key]);
            }

            
            curl_multi_close($mh);

CodePudding user response:

You could unpack the response object with a foreach. Something like

foreach($response as $value->$index){
    echo “value ” . $value . “ index: ” .  $index;
}

This is an example and should give you an idea how to debug your code.

I’m typing from a phone so I’m sorry

CodePudding user response:

You may need to put a comma after closing the Vehicles brackets

{
  "Vehicles": [
    {
      "ExternalVehicleId": "9uq0jz1c",
      "StockId": "1234",
      "Errors": []
    }
  ]
}
,
{
  "Finance": [
    {
      "ExternalVehicleId": "9uq0jz1d",
      "StockId": "4321",
      "Errors": []
    }
  ]

}

CodePudding user response:

Join the responses into a single JSON array, which you can then json_decode():

$responses = [];
foreach (array_keys($chs) as $key) {
  $responses[] = curl_multi_getcontent($chs[$key]);
}

$json = json_decode( '[' . join(',', $responses) . ']' );
var_dump($json);
  • Related