Wednesday, February 23, 2011

IP to location

If you are looking for a solution for converting IP address into its relevant location then here it is. I had done this conversion in 2 ways.

Method #1: First is purchasing an IP to location database from maxmind.com. In this you just need to dump the database in your server and use sql queries to get the location details of a particular IP. This perhaps, I hope to discuss in my future blogs in details.

Pros: The data retrieval is fast as it is in your database.
Cons: You need to update your purchased database from time and again for new IP's in the long run


Method #2: This is relatively much similar approach. You can refer to online API's. I used ipinfodb.com. In this you first need to get a api key. This feature is added new to the website. You need to register and they will mail you a API key. Use the API key in the following URL to get the location details in XML format:

http://api.ipinfodb.com/v2/ip_query.php?key=<YOUR_API_KEY_HERE>&ip=74.125.45.100&timezone=false

You will get the output in the following format:




In PHP you just need to make a CURL or file_get_contents() function to the above URL(containing your API key) and parse the xml output which is similar to the above sample.

For more details regarding output format in JSON or any other, you can check out the ipinfodb.com website itself.

Pros: You do not have to worry about the database updates for future IP's
Cons: The data fetching process may be slower than the 1st method as it is referring to an external website.

Hope this helps :)

1 comment: