<?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 i need to edit this output to this one (only with three rates (AED/AFN/AOA)). So, basically edit the json response in the section of rates. How can i do that?
angular.callbacks._0({
"disclaimer": "xx",
"license": "xx",
"timestamp": 1368136869,
"base": "USD",
"rates": {
"AED": 3.672819,
"AFN": 53.209,
"AOA": 107.953875,
}
})
To get JSON with Curl, you need to make an HTTP GET request and provide the Accept: application/json request header. The application/json request header is passed to the server with the curl -H command-line option and tells the server that the client is expecting JSON in response.
curl will not touch or parse the data that it sends, so you need to make sure it is valid JSON yourself. You can use multiple --json options on the same command line. This makes curl concatenate the contents from the options and send all data in one go to the server.
curl_exec(CurlHandle $handle ): string|bool. Execute the given cURL session. This function should be called after initializing a cURL session and all the options for the session are set.
To post JSON data using Curl, you need to set the Content-Type of your request to application/json and pass the JSON data with the -d command line parameter. The JSON content type is set using the -H "Content-Type: application/json" command line parameter. JSON data is passed as a string.
The $response
is not in correct json
format:
You must remove the unnecessary parts from 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);
// Get the 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With