Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON decoding from a URL

Tags:

json

php

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?

like image 363
Sebastian Avatar asked Jul 04 '11 11:07

Sebastian


1 Answers

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;
like image 79
Shashank Patel Avatar answered Oct 16 '22 02:10

Shashank Patel