how to get country name through ip using php


<?php
function countrynameFromIP($ipAddr)
{
ip2long($ipAddr)== -1 || ip2long($ipAddr) === false ? trigger_error("Invalid IP", E_USER_ERROR) : "";
$ipDetail=array(); //initialize a blank array
$xml = file_get_contents("http://api.hostip.info/?ip=".$ipAddr);
preg_match("@(\s)*(.*?)@si",$xml,$match);
$ipDetail['city']=$match[2];
preg_match("@(.*?)@si",$xml,$matches);
$ipDetail['country']=$matches[1];
//get the country name inside the node and
preg_match("@(.*?)@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;
}
echo $IPDetail=countrynameFromIP($_SERVER['REMOTE_ADDR']);
?>

Comments

Popular posts from this blog

php format date string short month

curl service example in php