views:

83

answers:

1

geonames.org got a web api you can use to get all the hierarchy for a city.

you just enter the id and you will get the data back in xml.

http://ws.geonames.org/hierarchy?geonameId=2657896 

i wonder how you fetch the url with php and javascript, and which one should i use.

cause the id's are in the mysql database. i will get them with php.

should i use file_get_contents, curl or fopen?

and what function in javascript? jquery $.post? heard that it can only access localhost.

would be great with some guidance here and even better with some code examples.

thanks!

A: 

should i use file_get_contents, curl or fopen?

you should use the one which suits your consuming code and which is enabled on your server. If the response is XML, you should consider fetching the file with simplexml_load_file(). Also see the Geonames PEAR package for this purpose.

and what function in javascript? jquery $.post? heard that it can only access localhost.

what you are refering to is the Same Origin Policy. There is ways around it, but then again, if you want to fetch the XML directly from JavaScript, e.g. client-side, then what is the PHP part for and why not get the webservice data as JSON instead?

Tell us a little bit more about how you intend to utilize the API and how your visitors are supposed to interact with it on your page. Also the part about the IDs being in the database is a bit vague.

EDIT

If you just want to get the data into your database, consider downloading the dump file from geonames.org and import it to your local database:

A daily GeoNames database dump can be downloaded in the form of a large worldwide text file (allCountries.zip). Additional country files are available for convenience. You find the files on our download server. Read the readme.txt for details. Postal codes are available as a separate download.

Gordon
basically i only want to get the hierarchy data into my database. so this is not for the users, only for me to complete my database data. so i could just do it with php? dont see why i should go through javascript when the flow looks like: mysql -> php -> geonames xml -> php -> mysql.
never_had_a_name
@fayer yes. That's why I asked why you want to use JavaScript at all. But if you just want the data, get the entire dump instead of querying the webservice. See update.
Gordon
i have ... but i cant get the hierarchy for a city with the dump. i have checked on the structure and all the relations, but cant find a way to get state and county for a city. thats why i want to use their web api for it.
never_had_a_name
"but cant find a way to get state and county for a city" - Then that's _another_ question. But to retrieve the data downloading the data dump file is certainly better than querying each and every single item.
VolkerK
@falyer hey, I think you had better use PHP for downloading. And when it comes to the problem of the hierarchy for a city. You may need some native code to reconstruct the tree data structure in the database.
xiao