How can I get data from this web service using PHP?
I need a simple PHP function to list the countries.
{
"valid":true,
"id":"0",
"data":{
"@type":"genericObjectArray",
"item":[
{
"id":"DE",
"description":"Deutschland"
},
{
"id":"ES",
"description":"España"
},
{
"id":"FR",
"description":"France"
},
{
"id":"PT",
"description":"Portugal"
},
{
"id":"UK",
"description":"United Kingdom"
},
{
"id":"US",
"description":"United States"
}
]
}
}
Assuming allow_url_fopen
is on in php.ini
(if not, use cURL library).
$json = file_get_contents('http://onleague.stormrise.pt:8031/OnLeagueRest/resources/onleague/Utils/Countries ');
$data = json_decode($json, TRUE);
$countries = array();
foreach($data['data']['item'] as $item) {
$countries[] = $item['description'];
}
CodePad.
Of course, handle if $json
is FALSE
(error with request).
Alternatively, if using >= PHP 5.3.
$countries = array_map(function($item) {
return $item['description'];
}, $data['data']['item']);
CodePad.
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