logo RSSTwitter

Free IP address geolocation tools
IP Address Geolocation XML API

The API returns the location of an IP address (country, region, city, zipcode, latitude, longitude) and the associated timezone in XML format. It can be used to query up to 25 IP in a single lookup. You can find below code samples with PHP, Javascript, Ruby, Python and ASP.

Usage
For city precision, do a query with the following API (if you omit the IP parameter it will check with your own IP) :
http://ipinfodb.com/ip_query.php?ip=74.125.45.100&timezone=false
To add timezone data, simply set the timezone parameter to true
http://ipinfodb.com/ip_query.php?ip=74.125.45.100&timezone=true
For multiples lookups (up to 25) in one request, the following API can be used :
http://ipinfodb.com/ip_query2.php?ip=74.125.45.100,206.190.60.37&timezone=false
Using the second API you can also replace IP addresses with domain names (note : 1 second pause for each domain) :
http://ipinfodb.com/ip_query2.php?ip=ebay.com,google.com&timezone=false
For country precision (faster), do a query with the following API :
http://ipinfodb.com/ip_query_country.php?ip=74.125.45.100
If you use this service on a regular basis, please consider making a donation. Our API are totally free but we need donations in order to pay for hosting and our 3 dedicated servers.
API list
API Precision Multiple request Timezone Domains lookups
ip_query.php City No Yes No
ip_query2.php City Yes (25) Yes Yes
ip_query_country.php Country No No No
ip_query2_country.php Country Yes (25) No Yes
Please use the appropriate API for your needs. You can help us keep the load low on our servers by making sure that :
  • If you only need the country name, avoid using the city precision API.
  • If you do one IP request at a time, use ip_query.php instead of ip_query2.php
  • If you don't need timezone data, set the timezone parameter to false (timezone=false) because it requires 2 additionnal queries on our side
  • If you track your visitors, avoid querying our API for all your page views (you can store the geolocation in a cookie, see below for an example)
Data accuracy

Over 99.5% on a country level and around 80% on a city level for the US within a 25 mile radius. The database used for this API is compiled from the free Maxmind CSV database (Geolite City) and rearranged with many scripts.

You can try a few lookups with our IP locator.

Servers location and uptime

We have 3 servers. Two are are load balanced & highly available (ldirectord + heartbeat setup). They are located in Montreal, Canada and used for the primary domain (ipinfodb.com). We also have a third server located in Los Angeles, US (backup.ipinfodb.com or us.ipinfodb.com). It can be used as the first server you query but keep in mind that it's not redundant as our main server in case of hardware failure. Our goal is to have over 99.99% uptime on the main server. 100% uptime can be reached by adding a failover server (eg backup.ipinfodb.com) in your code.

PHP example

We wrote a PHP class for our API. It can handle single or multiple IP/domain requests. You can download it here. Here is an example using all the features of the class :

<?php
include('geolocation.class.php');
 
//Load the class (city precision)
$geolocation = new geolocation(true);
 
//Load the class (country precision)
//$geolocation = new geolocation(false);
 
//Uncomment this line to show timezone data
//$geolocation->showTimezone(true);
 
//Uncomment this line if your server is on IPInfoDB whitelist ( no 2 queries/second limit). See http://ipinfodb.com/donate.php
//$geolocation->setWhitelist(true);
 
//Uncomment if you wish to use the US (L.A.) server as the primary
//$geolocation->useUSServer();
 
//Query timeout (in seconds) before switching to backup server
$geolocation->setTimeout(2);
 
//Set some "good" IP
$geolocation->setIP("66.135.205.14");
$geolocation->setIP("209.191.93.53");
 
//Set some "bad" IP and use the validator
$geolocation->setIP("999.191.93.53", true);
$geolocation->setIP("this is a test", true);
 
//Set some "good" domains
$geolocation->setDomain("google.com");
$geolocation->setDomain("ipinfodb.com");
 
//Set some "bad" domains and use the validator
$geolocation->setDomain("google", true);
$geolocation->setDomain("ipinfodb", true);
 
//Get errors and locations
$locations = $geolocation->getGeoLocation();
$errors = $geolocation->getErrors();
 
//Getting the first result
echo "<p>\n";
echo "<strong>First result</strong><br />\n";
if (!empty($locations[0]) && is_array($locations[0])) {
  foreach ($locations[0] as $field => $val) {
    echo $field . ' : ' . $val . "<br />\n";  
  }
}
echo "</p>\n";
 
//A dump of all results
echo "<p>\n";
echo "<strong>Dump of all results</strong><br />\n";
if (is_array($locations)) {
  foreach ($locations as $location) {
    echo var_dump($location) . "<br /><br />\n";    
  }
}
echo "</p>\n";
 
//Show errors
echo "<p>\n";
echo "<strong>Dump of all errors</strong><br />\n";
if (!empty($errors) && is_array($errors)) {
  foreach ($errors as $error) {
    echo var_dump($error) . "<br /><br />\n";    
  }
} else {
  echo "No errors" . "<br />\n";
}
echo "</p>\n";

Putting the geolocation data in a cookie
If you use our API to track your website visitors geolocation, we highly recommend that you put the geolocation data in a cookie (or in a database...). This way, you only query our servers for new visitors, not all page view. To achieve this, here is a quick sample in PHP how to set the country code in a cookie :
<?php
include_once('geolocation.class.php');
 
//Set geolocation cookie
if(!$_COOKIE["geolocation"]){
  $geolocation = new geolocation(true);
  $geolocation->setTimeout(2);
  $geolocation->setIP(getenv('REMOTE_ADDR'));
  list($visitorGeolocation) = $geolocation->getGeoLocation();
  if ($visitorGeolocation['Status'] == 'OK') {
    $data = base64_encode(serialize($visitorGeolocation));
    setcookie("geolocation", $data, time()+3600*24*7); //set cookie for 1 week
  }
}else{
  $visitorGeolocation = unserialize(base64_decode($_COOKIE["geolocation"]));
}
 
var_dump($visitorGeolocation);
Other languages and modules
If you find or write examples in other languages, please post them in the forum.
Accents encoding

Depending on the locales of your server, you might have trouble diplaying accent in country, region and city name. Using PHP, you might need to utf8_encode the answer from our API. For other languages, look at the documentation how to encode in UTF-8.

Query limit

We do not have a specific daily limit but queries that are at a rate faster than 2 per second will be put in "queue". If you stay below 2 queries/second everything will be normal. If you go over the limit, you will still get an answer for all queries but they will be slowed down to about 1 per second. This should not affect most users but for high volume websites, you can either use our IP database on your server or we can whitelist your IP for 5$/month (simply use the donate form and leave a comment with your server IP). Good programming practices such as not querying our API for all page views (you can store the data in a cookie or a database) will also help not reaching the limit.

API source code

You can find the code of the APIs on this page

Keeping in touch for updates

The best way to get updates and news is with our RSS feed or on Twitter

Support

For all non googable questions, visit the forum. We also offer custom integration.