<?php
function nacountryCityFromIP($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://services.ipaddresslabs.com/iplocation/locateip?key=demo&ip=".$ipAddr);
//get the city name inside the node <gml:name> and </gml:name>
preg_match("@<response>(\s)*<city>(.*?)</city>@si",$xml,$match);
//assing the city name to the array
$ipDetail['citya']=$match[2];
//get the country name inside the node <countryName> and </countryName>
preg_match("@<country_name>(.*?)</country_name>@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("@<latitude>(.*?)</latitude>@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;
}
{
//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://services.ipaddresslabs.com/iplocation/locateip?key=demo&ip=".$ipAddr);
//get the city name inside the node <gml:name> and </gml:name>
preg_match("@<response>(\s)*<city>(.*?)</city>@si",$xml,$match);
//assing the city name to the array
$ipDetail['citya']=$match[2];
//get the country name inside the node <countryName> and </countryName>
preg_match("@<country_name>(.*?)</country_name>@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("@<latitude>(.*?)</latitude>@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;
}
$IPDetail=nacountryCityFromIP('115.241.52.189');
echo $IPDetail['country']."--"; //country of that IP address
echo $IPDetail['citya']."--"; //outputs the IP detail of the city
echo $IPDetail['country_code']."--"; //outputs the Country code
?>
This comment has been removed by the author.
ReplyDelete