Wednesday, August 29, 2012

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']);
?>
-------------------------------------------------------------
if you helpful my code please donate some few amount to
developing and free to post.
-------------------------------------------------------------

No comments:

Post a Comment