PHP Geo IP Address Lookup
Today I searched on how to get the city of a user based on their ip address. I was looking to do this using PHP and preferably for free. There was quiet a bit out there that had a free database you could download to cross reference that would return the country. However, we wanted to be able to get the city and state as well. After quiet a bit of googling, I came across a free API from ipgp.net. They allow you to post the ip to a URL and they return an XML packet with the county, a path to the image of the country’s flag, city, state, isp, latitude, and longitude. They have an example on how to do this on their page. We created our own class below for your use that is a little cleaner. It can be included in your php scripts and you don’t have to worry about downloading anything into MySQL.
/*
Provided by http://www.todayisearched.com
*/
class GeoIP {
/*
Returns all possible Geo Ip information in an associative array. The following pieces of information
are returned:
IP - IP ADDRESS LOOKED UP
CODE - COUNTRY CODE
COUNTY - COUNTRY NAME
FLAG - PATH TO IMAGE OF THE COUNTRY'S FLAG
CITY - CITY NAME
REGION - STATE NAME
ISP - ISP NAME
LAT - LATITUDE CORDINATE
LNG - LONGITUDE CORDINATE
USAGE:
$ipinfo = GeoIP :: getGeoArray('xxx.xxx.xxx.xxx');
echo $ipinfo['CITY'] . ', ' . $ipinfo['STATE'];
*/
public static function getGeoArray($ip) {
$file = "http://www.ipgp.net/api/xml/". $ip;
$xml_parser = xml_parser_create();
$fp = fopen($file, "r");
$data = fread($fp, 80000);
xml_parse_into_struct($xml_parser, $data, $vals);
$iplookup = array();
foreach ($vals as $v) {
if (isset($v['tag']) && isset($v['value'])) {
$iplookup[$v['tag']] = $v['value'];
}
}
xml_parser_free($xml_parser);
fclose($fp);
return $iplookup;
}
//shortcut to get the city name
public static function getCity($ip) {
$a = self :: getGeoArray($ip);
return $a['CITY'];
}
//shortcut to get the state name
public static function getState($ip) {
$a = self :: getGeoArray($ip);
return $a['REGION'];
}
}
Follow these steps to implement the above script:
- Copy the script above and save it into a php file on your server
- Add include_once(‘Path To The PHP File Saved On #1′);
- Use the code snippet below in your php to obtain geoip information.
include_once "geoip.php";
$ipinfo = GeoIP :: getGeoArray($_SERVER['REMOTE_ADDR']);
echo 'Hello, Your City And State Is ' . $ipinfo['CITY'] . ', ' . $ipinfo['REGION'];
Thanks http://www.ipgp.net for providing this invaluable service!

Thanks! This is great! I’ve been searching for a free API that returns GeoIP data, and this is the first free one that returns tiny piece of data (XML) and really returns the City. AWESOME!!!
Hello,
There is a problem either with the PHP script that was posted on ipgp.net or with the ip database itself. I am using the script on my webpage and everything worked perfectly until some weeks ago. Since then I get following error: “Error on line 8″ (XML_PARSE). What is really curious is that for instance when I have this ip: 79.113.250.110 no error appears but when using ip: 79.113.212.4 there is this Error on line 8 message. From my point of view there has to be a problem with the PHP script but I can’t figure out what it is. I am using the original script from IPGP.net. Anyway, I have tryied to use also the script from above but still I get the error “UNDEFINED INDEX”. Please look for yourself and let me know what do you think.
Thank you.
Hi Mihai,
I am not sure why you are getting that error. I just went to this URL http://www.ipgp.net/api/xml/79.113.212.4 and it brought up the XML for that IP just fine. I would try that same url in a browser from the computer you are trying to connect to the API with and see if it is returning the XML ok. I am guessing the xml came back malformed or it could not connect to their server for some reason.
there is some problem in xml file.
it return attribute .. /a attribute.
IpLookup
Ip 114.143.165.223 /a /Ip
so cant load xml file. and wont get desite output
I took a quick look and tried to query your ip using the following URL:
http://www.ipgp.net/api/xml/114.143.165.223
It spit back:
“The API is now available only with an api key. Please use the form at: ipgp.net/get-api-key to get your API key”
It looks like you will have to signup to get an API key. They have a free one that allows 3,000 queries per day. http://www.ipgp.net/get-api-key/
When I have a chance, I will update the post on how to use this new key that they have.
Paul