We sometimes need to find out the location of an IP address so that we can utilize it
in our website in some way. This article will show you how to obtain the location of an IP
through the API provided by locatorhq.com. The usage of this API is quite simple and free to use. I will show you how to use the API and implement the code on a webpage.
How to obtain an API key?
First of all, you need to visit http://www.locatorhq.com and sign up with your name and email address. Don't worry, it's free. An API key will
be sent to your email address with your username. Once you have that information, you can start using the API.
How to use the API?
This is how a basic locatorHQ API call looks like:
user = your username registered with the website
key = your API key
ip = ip address to look up
The other parameter is
format with which you can specify the format in which you want the output in.
As of now, the JSON format is not yet available I guess. If you don't use the
format parameter, the API dumps the output in TEXT format.
Let's take an example here, let's look up for the location for Google's IP address. Type http://api.locatorhq.com/?user=Username&key=APIKEY&ip=126.96.36.199
into your browser.
Here is the output:
US,United States,California,Mountain View,34.305,-86.2981
Here is the breakup of the output:
Now hope you're with me so far. We will see how to use the XML format.
Here is how you mention the format: http://api.locatorhq.com/?user=Username&key=APIKEY&ip=188.8.131.52&format=xml
<countryName>United States</countryName><region>California</region><city>Mountain View</city>
Alright, now we know how to use the API so we're ready to use it on our webpage using PHP. So here is the PHP code that will tell the visitor of the webpage about his/her location:
$ipaddress = $_SERVER['REMOTE_ADDR']; $locationstr="http://api.locatorhq.com/?user=myusername&key=myAPIkey&ip=";
$locationstr = $locationstr.$ipaddress."&format=xml";
$xml = simplexml_load_file($locationstr);
$countrycode = $xml->countryCode; $countryname = $xml->countryName; $region = $xml->region; $city = $xml->city; $lattitude = $xml->lattitude; $longitude = $xml->longitude;
echo "Location information for <b>".$ipaddress."<br/><br/>";
echo "Country Code: ".$countrycode."<br/>";
echo "Country: ".$countryname."<br/>";
echo "Region: ".$region."<br/>";
echo "City: ".$city."<br/>";
echo "Lattitude: ".$lattitude."<br/>";
echo "Longitude: ".$longitude."<br/>";
How is this useful to me?
Well, you can actually use this in any way you wish and you can use your imagination but here are some common uses:
- You can personalize the webpage and present it in the language from where the visitor is from.
can use this information to monitor the web traffic coming to your
website. This helps you customize the website and allows you to present
the products that sells more in that region.
- If you have
advertisement on your website then you can present those advertisements
that are applicable and more popular in the region from where the
visitor is from.
- You can help the visitor with the nearest
stores and service center locations with this information, which avoids a
lot of hassle for the customer searching for this information.
regards to security, for example, your system administrator is aware
of a hacker's group in particular area set their minds to deface any
website on their way. You can use this information to block those IP
addresses if something suspicious is happening.
There are lot more things you can do with this information.
Points of interest
When I first saw this thing and used it, it was simply amazing for me.
I really like this so I decided to share this information. I know many people out there might know about this already but
for those who don't, start using it. It is simply awesome. I am no
expert in anything, so your comments and suggestion are valuable to me.
Thanks for reading.
If you have any questions or suggestions, please email to me at firstname.lastname@example.org.