This topic is locked
[SOLVED]

 REST API Parsing Example

4/29/2020 4:39:00 PM
PHPRunner General questions
S
salus1 authorDevClub member

After trying a number of APIs I'm finding the only thing they have in common is that they are all different and, if they return multi-level results, will need additional manipulation to be useful.
Would it be possible to get an example of the sanctioned method to parse multi-level results?
For instance the URL below will load countries data...
https://restcountries.eu/rest/v2/all?fields=name;capital;currencies;region;subregion;population;area
...but if you map all the returned fields in the Create List Operation the 3 currency related columns will not import into the resulting view.
I built an intermediary PHP file (below) that takes the result and parses it in to a single level table. It works but it seems pretty clunky. What's the right way to accomplish this, I'm pretty sure I'm going about it all wrong.
header("Access-Control-Allow-Origin: *");

header("Content-Type: application/json; charset=utf8mb4");

header("Cache-Control: no-cache, must-revalidate");

$curl = curl_init();

curl_setopt_array($curl, array(

CURLOPT_URL => "https://restcountries.eu/rest/v2/all?fields=name;capital;currencies;region;subregion;population;area",

CURLOPT_RETURNTRANSFER => true,

CURLOPT_TIMEOUT => 30,

CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,

CURLOPT_CUSTOMREQUEST => "GET",

CURLOPT_HTTPHEADER => array("cache-control: no-cache"),

));

$response = curl_exec($curl);

$response = str_replace("}],\"name","}],\"country",$response);

$response = str_replace("[","",$response);

$response = str_replace("{\"currencies\":","",$response);

$response = str_replace("}],",",",$response);

$response = str_replace("},{,\"country",",\"country",$response);

$response = "[".$response;

curl_close($curl);

print_r($response);

?>

Admin 4/29/2020

Could you elaborate a bit - what would you like to do with these results once retrieved? Like display it in a certain way?
Also, are you talking about REST Views in PHPRunner or about doing something in your code?

S
salus1 authorDevClub member 4/29/2020

Thanks for your reply. I'd like to read the result of query into a single PHPRunner REST view.
When I create a view based on the response from...
https://restcountries.eu/rest/v2/all?fields=name;capital;currencies;region;subregion;population;area
...and in the Create List Operation I add all 9 fields it will omit the data from the 3 currency-related fields.
The Name, Symbol and Code date are missing...
https://www.customdataservices.net/restapisdb/countries_list.php
...as they are on a different level in the JSON hierarchy.
I think it's something basic I'm missing in the Field Properties mapping (/currencies//code) but...

Admin 4/30/2020

I see what you saying.
Right now we can only work with one currency per country. Here are paths to currency fields that you can use:

/currencies/0/code

/currencies/0/name

*/currencies/0/symbol
A bit later we can make every currency a single record. Zimbabwe will have eight records on the list in this case etc.

S
salus1 authorDevClub member 4/30/2020

That's brilliant and works perfectly. So weird how obvious the answer is once you have the answer.
Thanks (yet again)!!