开发者

how to use web api with javascript/php?

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

you jus开发者_StackOverflow中文版t 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜