Skip to content
Advertisement

How to decode JSON when using PHP multi_curl

I’m trying to access data on the internet via an AJAX and using PHP. I’ve managed to access to data, however, it is JSON encoded. I want to decode the results.

PHP File:

$executionStartTime = microtime(true) / 1000;


// build the individual requests, but do not execute them
$ch_1 = curl_init('http://api.geonames.org/countryInfoJSON?formatted=true&lang=en&country='. $_REQUEST['iso'].'&username=PRIVATE&style=full');
$ch_2 = curl_init('https://restcountries.eu/rest/v2/alpha/'.$_REQUEST['iso']);
$ch_3 = curl_init('https://openexchangerates.org/api/latest.json?app_id=PRIVATE');

curl_setopt($ch_1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_3, CURLOPT_RETURNTRANSFER, true);

// build the multi-curl handle, adding both $ch
$mh = curl_multi_init();
curl_multi_add_handle($mh, $ch_1);
curl_multi_add_handle($mh, $ch_2);
curl_multi_add_handle($mh, $ch_3);


// execute all queries simultaneously, and continue when all are complete
$running = null;
do {
    curl_multi_exec($mh, $running);
 } while ($running);

//close the handles
curl_multi_remove_handle($mh, $ch_1);
curl_multi_remove_handle($mh, $ch_2);
curl_multi_remove_handle($mh, $ch_3);
curl_multi_close($mh);

// all of our requests are done, we can now access the results

$geoNames = curl_multi_getcontent($ch_1);
$restCountries = curl_multi_getcontent($ch_2);
$openExchange = curl_multi_getcontent($ch_3);

// Final Output
$output['status']['code'] = "200";
$output['status']['name'] = "OK";
$output['geoNames'] = $geoNames;
$output['restCountries'] = $restCountries;
$output['openExchange'] = $openExchange;

$output['status']['returnedIn'] = (microtime(true) - $executionStartTime) / 1000000 . " ms";

header('Content-Type: application/json; charset=UTF-8');

echo json_encode($output);

If you replace ‘json_encode($output)’ with ‘json_decode($output)’ or just ‘$output’ doesn’t work as it returns null with console.log().

JavaScript File:

function getInfo() {
getCountryBorder();
$.ajax({
    type: 'POST',
    url: 'libs/php/getInfo.php',
    data: {
        country: 'United Kingdom',
        iso: 'GB',
    },
    dataType: 'json',
    success: function (result) {

        console.log(result);
}
})
}

Data Returned is JSON encoded:

{status: {…}, geoNames: "{"geonames": [{↵  "continent": "EU",↵  "capital": …inentName": "Europe",↵  "currencyCode": "GBP"↵, "country": "United%20Kingdom"}]}", restCountries: "", openExchange: "", @@@…}

Desired Data Formatting is JSON decoded:

{status: {…}, geoNames: {"continent": "EU", "capital": continentName": "Europe", "currencyCode": "GBP"}, etc.}

Advertisement

Answer

The Final Output should be

// Final Output
$output['status']['code'] = "200";
$output['status']['name'] = "OK";
$output['geoNames'] = json_decode($geoNames, true);
$output['restCountries'] = json_decode($restCountries, true);
$output['openExchange'] = json_decode($openExchange, true);

The response from the remote api server is STRING json and we need to decode it before marge it to array

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement