Edit json answer from swirl

<?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); 

Output:

 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 for this (with only three bets (AED / AFN / AOA)). So basically edit the json answer in the betting section. How can i do this?

 angular.callbacks._0({ "disclaimer": "xx", "license": "xx", "timestamp": 1368136869, "base": "USD", "rates": { "AED": 3.672819, "AFN": 53.209, "AOA": 107.953875, } }) 
+6
source share
3 answers

$response does not match json rule:

You must remove 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); 
+3
source

It returns a response for jsonp. You will need this on the client side, but not on the server. You can try:

 http://openexchangerates.org/api/latest.json?app_id=xxxxx 

(i.e., omit the callback). Otherwise, take a look at their APIs to see if you can format the request differently to get pure JSON back without a callback.

If you absolutely cannot, then you can simply remove the bad parts:

 preg_match('/{.*}/', $response, $matches); $json = json_decode($matches[0]); 

Once you have valid JSON, this is a simple unset ting thing:

 unset($json->rates->XOF); //etc. 

You can also just write the content you need for some other object, if that is easier.

You can take a look at the API to see if you can only return specific bids.

+3
source

Read the JSON string into the PHP mapping array using json_decode , select only the elements you need, compose a new array with them, and finally re-execute it using json_encode .

Note. This method is more reliable than regex / string based, since it makes sense that it is processed, which allows mutations in the JSON structure if the elements you need are in their places.

To select items, use:

 $keep = array("AED", "AFN", "AOA"); $data["rates"] = array_intersect_key($data["rates"], array_flip($keep)); 
+1
source

Source: https://habr.com/ru/post/944665/


All Articles