Wednesday 28 November 2012

IP2Location – Tracking Geo locations based on IP address



Tracking of locations based on IP is a common requirement for most of the web application. A lot of APIs are providing these facilities. A good option is http://ipinfodb.com
Here we are explaining how to integrate this API with normal PHP website. We have converted the code to a codeigniter library, so that you can use this for you codeigniter application also.
Signing up for API keys
1) An API key is required for setting up this. So first you must register with http://ipinfodb.com/register.php to get an API key. An email will send to your address. Up on activation, you will get an API key displayed in the activation window.
2) Please note down the key once you got this, otherwise the this will not be available again with the same Email address.
Integration with PHP
1) http://ipinfodb.com providing a php class library to connect with their APIs. You can download it from the parent website. It is also available here.
2) Extract the zip and move files to your desired location. After that, you can set the API key in ip2location/index.php. For this please refer to the line:
$ipLite->setKey(your_api_key);
3) Now the API is ready to work. You can execute the script and use it for your application
Integration with Codeigniter
2) Move the file ‘ip2location.php’ to your application/libraries folder.
3) Now you can load the library from your controller as:
$this->load->library(‘ip2location’);
4) Then you need to set the API key as below.
$this->ip2location->setKey(your_api_key);
5) Once it is set, you can call the API for tracking the IP. This will be like:
$locations = $this->ip2location->getCity();
6) $locations is an array with all the details about the location. Printing the array will show all available details.
That’s it.
The above one is free using IP2Location Lite version. But they have a commercial version which provides more accuracy.
For more details about the API and ip address database please visit http://ipinfodb.com
If you have any questions, please feel free to email us at support@innovativewebz.com

No comments:

Post a Comment