I'm using an API which returns a JSON string:
http://api.geonames.org/findNearbyPlaceNameJSON?lat=51.9877644&lng=-1.47866&username=demo
The part I'm interested in is the city/town name (in this instance Hook Norton) which I'd like as a PHP variable. I understand the JSON_decode()
function comes into play somewhere, but how do I access the output of the API?
Try this.
$json = file_get_contents('http://api.geonames.org/findNearbyPlaceNameJSON?lat=51.9877644&lng=-1.47866&username=demo');
$data = json_decode($json,true);
$Geonames = $data['geonames'][0];
echo "<pre>";
print_r($Geonames);
exit;
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