php - Edit json response from curl response -
<?php $json_url = "http://openexchangerates.org/api/latest.json?app_id=xxxxx&callback=angular.callbacks._0"; $curl = curl_init(); curl_setopt($curl, curlopt_url, $json_url); curl_setopt($curl, curlopt_header, false); curl_setopt($curl, curlopt_returntransfer, 1); curl_setopt($curl, curlopt_http_version, curl_http_version_1_1); $response = curl_exec($curl); $jsonstring = json_encode($response, true); $data=json_decode($jsonstring); echo '<pre>',print_r($data),'</pre>'; $status = curl_getinfo($curl); curl_close($curl); the output is:
angular.callbacks._0({ "disclaimer": "xx", "license": "xx", "timestamp": 1368136869, "base": "usd", "rates": { "aed": 3.672819, "afn": 53.209, "all": 107.953875, "aoa": 96.358934, "ars": 5.214887, .... "xof": 501.659003, "xpf": 91.114876, "zmk": 5227.108333, "zmw": 5.314783, "zwl": 322.387247 } }) but need edit output 1 (only 3 rates (aed/afn/aoa)). so, edit json response in section of rates. how can that?
angular.callbacks._0({ "disclaimer": "xx", "license": "xx", "timestamp": 1368136869, "base": "usd", "rates": { "aed": 3.672819, "afn": 53.209, "aoa": 107.953875, } })
the $response not in correct json format:
you must remove unnecessary parts it:
$jsonstring= substr($response, 21, -1); you can do:
<?php $json_url = "http://openexchangerates.org/api/latest.json?app_id=5bf388eb6f7e40209b9418e6be44f04b&callback=angular.callbacks._0"; $curl = curl_init(); curl_setopt($curl, curlopt_url, $json_url); curl_setopt($curl, curlopt_header, false); curl_setopt($curl, curlopt_returntransfer, 1); curl_setopt($curl, curlopt_http_version, curl_http_version_1_1); $response = curl_exec($curl); $jsonstring= substr($response, 21, -1); $data=json_decode($jsonstring, true); // list of rates $rates = $data['rates']; $new_rates = array(); $new_rates['aed'] = $rates['aed']; $new_rates['afn'] = $rates['afn']; $new_rates['aoa'] = $rates['aoa']; $data['rates'] = $new_rates; echo 'angular.callbacks._0('.json_encode($data).')'; $status = curl_getinfo($curl); curl_close($curl);
Comments
Post a Comment