A blog about PHP, Ajax, JavaScript, CSS and Web 2.0

Getting country , city name from IP address in PHP

Yesterday, miaki asked me how can we get the country name from the IP address in PHP. Today, I’ve come up with the answer of this question. I’ve used the API from hostip.info to fetch the country name , city name and country code from the given IP address. I’ve mad this function in PHP which uses XML response from hostip.info and extracted country name, city name and country code using regular expression.

Function to return country, city name from IP address in PHP

function countryCityFromIP($ipAddr)
{
//function to find country and city from IP address
//Developed by Roshan Bhattarai http://roshanbh.com.np
//verify the IP address for the
ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
$ipDetail=array(); //initialize a blank array
//get the XML result from hostip.info
$xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);
//get the city name inside the node <gml:name> and </gml:name>
preg_match("@<Hostip>(\s)*<gml:name>(.*?)</gml:name>@si",$xml,$match);
//assing the city name to the array
$ipDetail['city']=$match[2];
//get the country name inside the node <countryName> and </countryName>
preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);
//assign the country name to the $ipDetail array
$ipDetail['country']=$matches[1];
//get the country name inside the node <countryName> and </countryName>
preg_match("@<countryAbbrev>(.*?)</countryAbbrev>@si",$xml,$cc_match);
$ipDetail['country_code']=$cc_match[1]; //assing the country code to array
//return the array containing city, country and country code
return $ipDetail;
}

As you can see, I’ve documented all the PHP code and I don’t think I need explain anymore about that code. Just notice that, this function returns the array containg three key elements “country” , “city” and “country_code”. Each elements have the value of city, country and country code.

Now, look the the how we can use the above function in PHP,

$IPDetail=countryCityFromIP('12.215.42.19');
echo $IPDetail['country']; //country of that IP address
echo $IPDetail['city']; //outputs the IP detail of the city

Notice that the above PHP function returns the array containing the country , city and country code from IP Address and we can use them in PHP. If you want to know how to get IP address in PHP, you can check this post how you can get real IP address in PHP.

I wrote a script a while back using the html that hostip returned, thanks for the easy read on an xml version. For those that find hostip.info returns inaccurate info, I suggest http://maxmind.com as an very accurate but pay alternative.

@Krasimir – You’ve to pay some bucks to maxmind for more accurate information retrieval

Someone

July 18, 2008

This /isn’t/ using php to derive country names. This is only a method of parsing xml using php for a site that may or may not be operational tomorrow. I’d see this as a fairly temporary solution, at best.

ya seems that their API is returning wrong value for this ip address 202.79.63.59. Hostip.info is returning Hydarabad, India but when I checked the same from ip2location.com it is showing it is from Nepal.

You insert an explicit numeric IP address in the countryCityFromIP(‘66.96.134.24′). I get errors when I use a variable. Not being a PHP expert, this is confusing and I get errors no matter whatever I use. So, how do I use an address derived from the script?

Great! Does anybody know if there are restrictions for using this type of service. As it is free I suppose it has its limitations!

Krishan

February 27, 2009

Does this work now?

Vikas

March 18, 2009

Hi Roshan,

Thanks for the script.
I have another doubt here. You are making use of webservice to get the country name. Now suppose tomorrow, if the service provides changes the host name or if they stop providing the details, then we will not be able to get the details. Can we rely on the host name (http://api.hostip.info/?ip=) through which you are getting the details ?

Thanks

Tim

April 13, 2009

You can get the free API from http://www.ip2location.com. It is more accurate that the free API at hostip as we’ve tested for some times.

does not seem to work when it’s not allowed to include /open a file from a different server.

Olaf

venkat20

April 22, 2009

I have found the Ip details for my home network on the site named as http://www.ip-details.com.It is very useful for me .I found the comnection speed like the uploadind speed,downloadind speed etc on the same website ,Domain name given means it will display the address of the ip server where it is located etc ..I want to know that all from the website above mentioned …another thing is it will display the map if the search loaction u have entered on search ..

I try to test it on my sites. I try it via localhost. But, why doesn’t it work?

raj

November 4, 2009

HI roshan ,

I m from hyderabad india i m not able to get the zip code other than this i can get the country name city name every thing works fine is this issue with the only city from india or other countries also face the same problem? as i m facing problem by not retriving the IPaddress