从IP地址查找国家/地区名称[重复]

This question already has an answer here:

$user_ip_address=$_SERVER['REMOTE_ADDR'];

I find out the ip address by using the above code..How can i find out the country name using this ip address?

Thanks

</div>

You need geo location databases see www.maxmind.com they are offering it for free.

Here is the link to the API on hostip.info: http://www.hostip.info/use.html

For the queries that fail, you could try falling back to another site for the query, such as hostip.info, who happen to have a decent API and seem friendly:

http://api.hostip.info/country.php?ip=4.2.2.2

returns:

US

U will need ip 2 country database Look here: http://phpweby.com/software/ip2country

or use external Service like: http://freegeoip.net/static/index.html

example:

http://freegeoip.net/json/85.152.4.144

You can use something like this :

function countryCityFromIP($ipAddr)
{
   ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";$ipDetail=array();

   $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);

   $ipDetail['city']=$match[2];

   preg_match("@<countryName>(.*?)</countryName>@si",$xml,$matches);

   $ipDetail['country']=$matches[1];

   preg_match("@<countryAbbrev> (.*?)</countryAbbrev>@si",$xml,$cc_match);$ipDetail['country_code']=$cc_match[1]; //assing the country code to array

   return $ipDetail;
}
 print_r(countryCityFromIP('202.53.71.151'));