Usage
For city precision, simply do a query at this address (if you omit the IP parameter it will check with your own IP) :http://ipinfodb.com/ip_query.php?ip=74.125.45.100
For country precision (faster), simply do a query at this address (if you omit the IP parameter it will check with your own IP) :
http://ipinfodb.com/ip_query_country.php?ip=74.125.45.100
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 on this page.
Servers uptime
Our servers are load balanced & highly available (ldirectord + heartbeat setup). Our goal is to have over 99.99% uptime.
Output
This is a sample of the output you will get in XML with city precision :<?xml version="1.0" encoding="UTF-8"?> <Response> <Ip>74.125.45.100</Ip> <Status>OK</Status> <CountryCode>US</CountryCode> <CountryName>United States</CountryName> <RegionCode>06</RegionCode> <RegionName>California</RegionName> <City>Mountain View</City> <ZipPostalCode>94043</ZipPostalCode> <Latitude>37.4192</Latitude> <Longitude>-122.057</Longitude> <Timezone>-8</Timezone> <Gmtoffset>-8</Gmtoffset> <Dstoffset>-7</Dstoffset> </Response>
PHP example (procedural)
(See below for object oriented examples)<?php function locateIp($ip){ $d = file_get_contents("http://www.ipinfodb.com/ip_query.php?ip=$ip&output=xml"); //Use backup server if cannot make a connection if (!$d){ $backup = file_get_contents("http://backup.ipinfodb.com/ip_query.php?ip=$ip&output=xml"); $answer = new SimpleXMLElement($backup); if (!$backup) return false; // Failed to open connection }else{ $answer = new SimpleXMLElement($d); } $country_code = $answer->CountryCode; $country_name = $answer->CountryName; $region_name = $answer->RegionName; $city = $answer->City; $zippostalcode = $answer->ZipPostalCode; $latitude = $answer->Latitude; $longitude = $answer->Longitude; $timezone = $answer->Timezone; $gmtoffset = $answer->Gmtoffset; $dstoffset = $answer->Dstoffset; //Return the data as an array return array('ip' => $ip, 'country_code' => $country_code, 'country_name' => $country_name, 'region_name' => $region_name, 'city' => $city, 'zippostalcode' => $zippostalcode, 'latitude' => $latitude, 'longitude' => $longitude, 'timezone' => $timezone, 'gmtoffset' => $gmtoffset, 'dstoffset' => $dstoffset); } //Usage example $ip = "74.125.45.100"; $ip_data = locateIp($ip); echo "IP : " . $ip_data['ip'] . "\n"; echo "Country code : " . $ip_data['country_code'] . "\n"; echo "Country name : " . $ip_data['country_name'] . "\n"; echo "Region name : " . $ip_data['region_name'] . "\n"; echo "City : " . $ip_data['city'] . "\n"; echo "Zip/postal code : " . $ip_data['zippostalcode'] . "\n"; echo "Latitude : " . $ip_data['latitude'] . "\n"; echo "Longitude : " . $ip_data['longitude'] . "\n"; echo "Timezone : " . $ip_data['timezone'] . "\n"; echo "GmtOffset : " . $ip_data['gmtoffset'] . "\n"; echo "DstOffset : " . $ip_data['dstoffset'] . "\n"; ?>
PHP example (object oriented)
We wrote a PHP class for our API. It can handle single or multiple IP/domain requests. You can download it here. Here is a simple example :<?php include('geolocation.class.php'); //Load the class (city precision) $geolocation = new geolocation(true); //Load the class (country precision) //$geolocation = new geolocation(false); //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";
Other languages and modules
Javascript examplesRuby examples
ASP examples
OSCommerce module
Drupal module
Joomla extension(free non commercial)
If you find examples in other languages, please post them in the forum.
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 ipinfodb.com 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 //The function that query IPInfoDB.com function visitorCountry(){ $ip = getenv('REMOTE_ADDR'); $d = file_get_contents("http://www.ipinfodb.com/ip_query_country.php?ip=$ip&output=xml"); //Use backup server if cannot make a connection if (!$d){ $backup = file_get_contents("http://backup.ipinfodb.com/ip_query.php?ip=$ip&output=xml"); $answer = new SimpleXMLElement($backup); if (!$backup) return false; // Failed to open connection }else{ $answer = new SimpleXMLElement($d); } return $answer->CountryCode; } //Set geolocation cookie if(!$_COOKIE["geolocation"]){ $visitor_country = visitorCountry(); setcookie("geolocation", $visitor_country); }else{ $visitor_country = $_COOKIE["geolocation"]; }
Multiple queries in one requests and domain name location
It is now possible to do multiple queries (max 25 IP) using this API for city precision:http://ipinfodb.com/ip_query2.php?ip=74.125.45.100,206.190.60.37
http://ipinfodb.com/ip_query2_country.php?ip=74.125.45.100,206.190.60.37
http://ipinfodb.com/ip_query2.php?ip=ebay.com
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.
Timezones
Timezone data is from Geonames. Basically, the timezone field is the standard timezone. The gmtOffset/dstOffset fields names are a little misleading --> gmtOffset: offset to GMT at 1st January and dstOffset: offset to GMT at 1st July. Please refer to Geonames.org for more info about timezones.
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 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

