views:

405

answers:

4
+1  Q: 

GeoLocation API

Hello all,

I have just come across this great API: http://ipinfodb.com/ip%5Flocation%5Fapi.php

However, I would like to also retrieve the continent. Is this even possible as I can not find it.

Maybe there is another API that is freely available that will offer city, country and continent? I have tried Googles AJAX API but they don't offer continent either.

Maybe I going to have to just convert what I already have into continents, how?!

I hope this isn't too subjective!

Thanks all

+3  A: 

Why don't you map each CountryCode**XX**CountryCode to the respective continent? Shouldn't be too hard since there are only about 200 countries and 5 or 7 continents.

EDIT: I have no knowledge of such prepared database in PHP, but here is a json_encode'd array for all the countries with the country code as key and continent name as value:

$json = '{"AD":"Europe","AE":"Asia","AF":"Asia","AG":"North America","AI":"North America","AL":"Europe","AM":"Asia","AN":"North America","AO":"Africa","AQ":"Antarctica","AR":"South America","AS":"Australia","AT":"Europe","AU":"Australia","AW":"North America","AZ":"Asia","BA":"Europe","BB":"North America","BD":"Asia","BE":"Europe","BF":"Africa","BG":"Europe","BH":"Asia","BI":"Africa","BJ":"Africa","BM":"North America","BN":"Asia","BO":"South America","BR":"South America","BS":"North America","BT":"Asia","BW":"Africa","BY":"Europe","BZ":"North America","CA":"North America","CC":"Asia","CD":"Africa","CF":"Africa","CG":"Africa","CH":"Europe","CI":"Africa","CK":"Australia","CL":"South America","CM":"Africa","CN":"Asia","CO":"South America","CR":"North America","CU":"North America","CV":"Africa","CX":"Asia","CY":"Asia","CZ":"Europe","DE":"Europe","DJ":"Africa","DK":"Europe","DM":"North America","DO":"North America","DZ":"Africa","EC":"South America","EE":"Europe","EG":"Africa","EH":"Africa","ER":"Africa","ES":"Europe","ET":"Africa","FI":"Europe","FJ":"Australia","FK":"South America","FM":"Australia","FO":"Europe","FR":"Europe","GA":"Africa","GB":"Europe","GD":"North America","GE":"Asia","GF":"South America","GG":"Europe","GH":"Africa","GI":"Europe","GL":"North America","GM":"Africa","GN":"Africa","GP":"North America","GQ":"Africa","GR":"Europe","GS":"Antarctica","GT":"North America","GU":"Australia","GW":"Africa","GY":"South America","HK":"Asia","HN":"North America","HR":"Europe","HT":"North America","HU":"Europe","ID":"Asia","IE":"Europe","IL":"Asia","IM":"Europe","IN":"Asia","IO":"Asia","IQ":"Asia","IR":"Asia","IS":"Europe","IT":"Europe","JE":"Europe","JM":"North America","JO":"Asia","JP":"Asia","KE":"Africa","KG":"Asia","KH":"Asia","KI":"Australia","KM":"Africa","KN":"North America","KP":"Asia","KR":"Asia","KW":"Asia","KY":"North America","KZ":"Asia","LA":"Asia","LB":"Asia","LC":"North America","LI":"Europe","LK":"Asia","LR":"Africa","LS":"Africa","LT":"Europe","LU":"Europe","LV":"Europe","LY":"Africa","MA":"Africa","MC":"Europe","MD":"Europe","ME":"Europe","MG":"Africa","MH":"Australia","MK":"Europe","ML":"Africa","MM":"Asia","MN":"Asia","MO":"Asia","MP":"Australia","MQ":"North America","MR":"Africa","MS":"North America","MT":"Europe","MU":"Africa","MV":"Asia","MW":"Africa","MX":"North America","MY":"Asia","MZ":"Africa","NA":"Africa","NC":"Australia","NE":"Africa","NF":"Australia","NG":"Africa","NI":"North America","NL":"Europe","NO":"Europe","NP":"Asia","NR":"Australia","NU":"Australia","NZ":"Australia","OM":"Asia","PA":"North America","PE":"South America","PF":"Australia","PG":"Australia","PH":"Asia","PK":"Asia","PL":"Europe","PM":"North America","PN":"Australia","PR":"North America","PS":"Asia","PT":"Europe","PW":"Australia","PY":"South America","QA":"Asia","RE":"Africa","RO":"Europe","RS":"Europe","RU":"Europe","RW":"Africa","SA":"Asia","SB":"Australia","SC":"Africa","SD":"Africa","SE":"Europe","SG":"Asia","SH":"Africa","SI":"Europe","SJ":"Europe","SK":"Europe","SL":"Africa","SM":"Europe","SN":"Africa","SO":"Africa","SR":"South America","ST":"Africa","SV":"North America","SY":"Asia","SZ":"Africa","TC":"North America","TD":"Africa","TF":"Antarctica","TG":"Africa","TH":"Asia","TJ":"Asia","TK":"Australia","TM":"Asia","TN":"Africa","TO":"Australia","TR":"Asia","TT":"North America","TV":"Australia","TW":"Asia","TZ":"Africa","UA":"Europe","UG":"Africa","US":"North America","UY":"South America","UZ":"Asia","VC":"North America","VE":"South America","VG":"North America","VI":"North America","VN":"Asia","VU":"Australia","WF":"Australia","WS":"Australia","YE":"Asia","YT":"Africa","ZA":"Africa","ZM":"Africa","ZW":"Africa"}';

Just call it with:

echo '<pre>';
print_r(json_decode($json, true));
echo '</pre>';
Alix Axel
Thats exactly what I am trying to do, but surley there is a database that exists already or PHP has some inbuilt function to do this?
Abs
Awesome!! Where did you get that eyze and how accurate do you think it is?
Abs
AFAIK it's a reliable source, I found it available on a web service (http://services.sapo.pt/GIS/GetCountries) I just scraped it and translated the continent names to English.
Alix Axel
+2  A: 

There's only 200ish countries and only 6 continents, so I would question why you need a web service to determine that bit of info... unless you plan on getting a lot of users from countries so politically unstable they will change names or cease to exist before your app does. Just keep a static list.

Also, not all countries are on a continent. New Zealand for example, isn't part of Australia (according to Wikipedia at least).

If you can determine the user's country programmatically, you have enough information to look up their continent in a static list which you own. It would take you an hour to assemble that info yourself and put it in a database or XML file, and it will rarely change. You've already spent nearly that amount of time trying to find out how to determine the continent dynamically.

Rex M
What last bit info? You mean the continent? I need to determine this as I would like to do various functions based on the users continent location and then drill down by country, city etc. My question may be simple, but my line of thought isn't. :)
Abs
@Abs see my edit (moved comment into answer to help clarify)
Rex M
Thanks Rex. I understand. Its just that I am lazy and was hoping it would be prepared and all packaged up for me!
Abs
+1  A: 

Have a look MaxMind GeoLite Country and MaxMind GeoLite City.

If it's the continent you're mainly after (I don't know if GeoLite stores that) you could always map the country codes from the GeoLite database to the country codes of the world database which MySql uses as a sample database. That has the continent for each country stored.

karim79
I have tried these guys and their implementation is messy, accuracy isn't so great for the free feature. But you are right, its the best I've come across, but I need something better and lighter.
Abs
@Abs - the mysql world database has continents. If the GeoLite database(s) have country codes (haven't checked) this could work.
karim79
Good point. I think it does. Dear God, I am a simpleton! :)
Abs
A: 

Try http://www.geobytes.com/IpLocator.htm?GetLocation

I use their online interface all the time, never tried the API.

Shadow