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:
JavaScript
x
50
50
1
$executionStartTime = microtime(true) / 1000;
2
3
4
// build the individual requests, but do not execute them
5
$ch_1 = curl_init('http://api.geonames.org/countryInfoJSON?formatted=true&lang=en&country='. $_REQUEST['iso'].'&username=PRIVATE&style=full');
6
$ch_2 = curl_init('https://restcountries.eu/rest/v2/alpha/'.$_REQUEST['iso']);
7
$ch_3 = curl_init('https://openexchangerates.org/api/latest.json?app_id=PRIVATE');
8
9
curl_setopt($ch_1, CURLOPT_RETURNTRANSFER, true);
10
curl_setopt($ch_2, CURLOPT_RETURNTRANSFER, true);
11
curl_setopt($ch_3, CURLOPT_RETURNTRANSFER, true);
12
13
// build the multi-curl handle, adding both $ch
14
$mh = curl_multi_init();
15
curl_multi_add_handle($mh, $ch_1);
16
curl_multi_add_handle($mh, $ch_2);
17
curl_multi_add_handle($mh, $ch_3);
18
19
20
// execute all queries simultaneously, and continue when all are complete
21
$running = null;
22
do {
23
curl_multi_exec($mh, $running);
24
} while ($running);
25
26
//close the handles
27
curl_multi_remove_handle($mh, $ch_1);
28
curl_multi_remove_handle($mh, $ch_2);
29
curl_multi_remove_handle($mh, $ch_3);
30
curl_multi_close($mh);
31
32
// all of our requests are done, we can now access the results
33
34
$geoNames = curl_multi_getcontent($ch_1);
35
$restCountries = curl_multi_getcontent($ch_2);
36
$openExchange = curl_multi_getcontent($ch_3);
37
38
// Final Output
39
$output['status']['code'] = "200";
40
$output['status']['name'] = "OK";
41
$output['geoNames'] = $geoNames;
42
$output['restCountries'] = $restCountries;
43
$output['openExchange'] = $openExchange;
44
45
$output['status']['returnedIn'] = (microtime(true) - $executionStartTime) / 1000000 . " ms";
46
47
header('Content-Type: application/json; charset=UTF-8');
48
49
echo json_encode($output);
50
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:
JavaScript
1
17
17
1
function getInfo() {
2
getCountryBorder();
3
$.ajax({
4
type: 'POST',
5
url: 'libs/php/getInfo.php',
6
data: {
7
country: 'United Kingdom',
8
iso: 'GB',
9
},
10
dataType: 'json',
11
success: function (result) {
12
13
console.log(result);
14
}
15
})
16
}
17
Data Returned is JSON encoded:
JavaScript
1
2
1
{status: {…}, geoNames: "{"geonames": [{↵ "continent": "EU",↵ "capital": …inentName": "Europe",↵ "currencyCode": "GBP"↵, "country": "United%20Kingdom"}]}", restCountries: "", openExchange: "", @@@…}
2
Desired Data Formatting is JSON decoded:
JavaScript
1
2
1
{status: {…}, geoNames: {"continent": "EU", "capital": continentName": "Europe", "currencyCode": "GBP"}, etc.}
2
Advertisement
Answer
The Final Output should be
JavaScript
1
7
1
// Final Output
2
$output['status']['code'] = "200";
3
$output['status']['name'] = "OK";
4
$output['geoNames'] = json_decode($geoNames, true);
5
$output['restCountries'] = json_decode($restCountries, true);
6
$output['openExchange'] = json_decode($openExchange, true);
7
The response from the remote api server is STRING json and we need to decode it before marge it to array