Search code examples
phphtmljsongeolocationgeonames

How to use GeoNames Web Services using PHP


I have read this post and it looks very promising. However, the answer does not go in detail with regards the implementation.

Basically, given the latitude and longitude, how can I use the GeoNames Web Service to get the country. I would like the returned result to be stored in a variable so that I can then continue working with it as I wish.

Thanks in advance


Solution

  • Do you have to use javascript for that? Geonames doesn't seem to offer a javascript api for doing your exact query (see here: http://www.geonames.org/export/JSON-webservices.html). And remote file fetching via javascript is not a joy.

    If you could use php for that, it would be as easy as

    $country = file_get_contents("http://ws.geonames.org/countryCode?lat=49.03&lng=10.2");