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.
<?php
/**
* Geolocation API access
*
* @param string $ip IP address to query
* @param string $format output format of response
*
* @return string XML, JSON or CSV string
*/
function get_ip_location($ip, $format="xml") {
/* Set allowed output formats */
$formats_allowed = array("json", "xml", "raw");
/* IP location query url */
$query_url = "http://iplocationtools.com/ip_query.php?ip=";
/* Male sure that the format is one of json, xml, raw.
Or else default to xml */
if(!in_array($format, $formats_allowed)) {
$format = "xml";
}
$query_url = $query_url . "{$ip}&output={$format}";
/* Init CURL and its options*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $query_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
/* Execute CURL and get the response */
return curl_exec($ch);
}
$location_data = get_ip_location("209.85.153.104");
print_r($location_data);
?> |
Query response:
<Response>
<Ip>209.85.153.104</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>
</Response> |
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.
<?php
$location_data = get_ip_location("209.85.153.104");
$data = '';
try {
$data = @new SimpleXMLElement($location_data);
}
catch(Exception $e) {
echo "Error parsing XML.";
}
foreach($data as $key=>$value)
echo $key . " : " . $value . "\n";
?> |
14 Responses
1
david
April 14th, 2009 at 9:51 am
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.
sameer
April 14th, 2009 at 9:22 pm
The CSV format can make it easier to save the data to a file, which can later be opened in Excel.
3
ricky
April 14th, 2009 at 10:21 pm
it doesn’t work…..
error occurs….
Fatal error: Call to undefined function curl_init() in F:\www\iplocation\iplocation.php on line 28
sameer
April 14th, 2009 at 10:44 pm
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
I Have Candy, Get in The Van. - Nullamatix - Technology Made Simple
April 19th, 2009 at 5:33 pm
[...] objective with clear, thorough details. For example, their latest post at the moment covers a free geolocation api tool. I definitely look forward to the updates this site [...]
6
Tim
June 1st, 2009 at 12:49 am
Other than using the Web API, you can hosted the database in your own server.
We are using the IP2Location (not to confuse with the IPLocation) database from http://www.ip2location.com for local hosting and query.
7
Recommened PHP articles to improve your programming skills! | JortK.nl
June 4th, 2009 at 4:04 am
[...] Using the free Geolocation API tool with PHP PHP code to locate a geographical location using a IP address, very usefull API for all PHP developers! [...]
8
baek
June 18th, 2009 at 7:31 am
what if you want to use only the country name and city from the xml, how will the code change ?
sameer
June 18th, 2009 at 8:58 am
Instead of the foreach loop, use the following:
$country = (string) $data->CountryName;
$city = (string) $data->City;
10
sri
June 18th, 2009 at 11:50 pm
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.
11
Christian Harms
July 27th, 2009 at 12:15 pm
Instead of using only one location service try this to combile the results of up to five apis: http://united-coders.com/christian-harms/combining-http-and-javascript-apis-with-php
12
Christian Harms
July 28th, 2009 at 2:23 am
Btw. the API URL has changed!
13
Christian Harms
July 28th, 2009 at 2:23 am
The API URL has changed to ipinfodb.com!
14
Sunitha
October 24th, 2009 at 9:58 pm
Harms……Thanks a ton ! I was searching for the changed url but I could’nt find it anywhere. My search ends here. Thanks Again