0

I run curl_multi_getcontent function to send all api request at once. As a return I get one big array. Inside this array each url content is being save as string. Can someone please tell me how to change my code so the url content would return as array.

function multiRequest($data, $options = array()) {

$curly = array();
$result = array();
$mh = curl_multi_init();

$headers =   array(
    "api host",
    "api key"
);

foreach ($data as $id => $d) {

$curly[$id] = curl_init();

$url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
curl_setopt($curly[$id], CURLOPT_URL,            $url);
curl_setopt($curly[$id], CURLOPT_HTTPHEADER,$headers);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);

if (is_array($d)) {
  if (!empty($d['post'])) {
    curl_setopt($curly[$id], CURLOPT_POST,       1);
    curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
  }
}

if (!empty($options)) {
  curl_setopt_array($curly[$id], $options);
}

curl_multi_add_handle($mh, $curly[$id]);
}

$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
foreach($curly as $id => $c) {
$result[$id] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}
curl_multi_close($mh);

}

and below is example of a output which looks like array but is a sting.

Array
(
[0] => 
{
"api": {
    "results": 38,
    "fixtures": [
        {
            "fixture_id": 210282,
            "league_id": 761,
            "league": {
            "name": "NB II",
            "country": "Hungary",
            "logo": null,   
            },
            "event_date": "2019-08-04T17:00:00+00:00",
            "event_timestamp": 1564938000,
            "firstHalfStart": 1564938000,
            "secondHalfStart": 1564941600,

        },

        {
            "fixture_id": 210285,
            "league_id": 761,
            "league": {
            "name": "NB II",
            "country": "Hungary",
            "logo": null,
            },
            "event_date": "2019-08-07T15:30:00+00:00",
            "event_timestamp": 1565191800,
            "firstHalfStart": 1565191800,
        }
    ]           
    }
}
[1] => and so on
)

0 Answers0