Free Geolocation API tool

NOTE: http://www.ip2location.com have done away with the free api access from their site. So the following sample code will no longer work. They now provide a free sample database on their site and also a complete paid version.

iplocationtools.com offers a free geolocation API that lets you query with an ip address and get the location details such as city, country, zip, latitude, longitude etc. The site also offers a free MySQL database for the same if you would like to install it on your server. I’ve used CURL to wrap the API access. The complete function with a sample query and the response is shown below.


Query response:


	209.85.153.104
	OK
	US
	United States
	06
	California
	Mountain View
	94043
	37.4192
	-122.057

The function by default returns a XML response, but you can also get a response in json or csv format, as shown below.

/* Get response in 'json' format' */
$location_data = get_ip_location("209.85.153.104", "json");
{
"Ip" : "209.85.153.104",
"Status" : "OK",
"CountryCode" : "US",
"CountryName" : "United States",
"RegionCode" : "06",
"RegionName" : "California",
"City" : "Mountain View",
"ZipPostalCode" : "94043",
"Latitude" : "37.4192",
"Longitude" : "-122.057",
}

or

/* Get response in 'csv' format' */
$location_data = get_ip_location("209.85.153.104", "raw");
209.85.153.104,OK,US,United States,06,California,
Mountain View,94043,37.4192,-122.057

You can further parse the returned XML using SimpleXML.

$value)
        echo $key . " : " . $value . "\n";

?>

14 thoughts to “Free Geolocation API tool”

  1. I’m not sure what the use is of returning a plaintext format? If you want to fetch the information from the webservice why not output it in a structure that can be used in php.

  2. The CSV format can make it easier to save the data to a file, which can later be opened in Excel.

  3. it doesn’t work…..
    error occurs….
    Fatal error: Call to undefined function curl_init() in F:\www\iplocation\iplocation.php on line 28

  4. CURL is not enabled on your server. If you are running on your localhost enable CURL in your php.ini,

    extension=php_curl.dll

    OR

    you could use the ‘file_get_contents’ function in place of CURL in the ‘get_ip_location’ function above:

    $query_url = $query_url . “{$ip}&output={$format}”;

    $data = file_get_contents($query_url);

    if (!$data)
    return false;
    else
    return $data;

  5. what if you want to use only the country name and city from the xml, how will the code change ?

  6. Instead of the foreach loop, use the following:

    $country = (string) $data->CountryName;
    $city = (string) $data->City;

  7. hai,
    It’s nice.We can find the geo location of an ip-address using ip-details.It’s used to Search and find ip address’s,domain name host’s whois information including city, country, global latitude & longitude coordinates.it’s useful for me.

  8. Harms……Thanks a ton ! I was searching for the changed url but I could’nt find it anywhere. My search ends here. Thanks Again

Comments are closed.