Get country city Location from IP address
This is a sample in PHP of how you can integrate the answer in your application :
function locateIp($ip){
$d = file_get_contents("http://www.ipinfodb.com/ip_query.php?ip=$ip&output=xml");
//Use backup server if cannot make a connection
if (!$d){
$backup = file_get_contents("http://backup.ipinfodb.com/ip_query.php?ip=$ip&output=xml");
$answer = new SimpleXMLElement($backup);
if (!$backup) return false; // Failed to open connection
}else{
$answer = new SimpleXMLElement($d);
}
$country_code = $answer->CountryCode;
$country_name = $answer->CountryName;
$region_name = $answer->RegionName;
$city = $answer->City;
$zippostalcode = $answer->ZipPostalCode;
$latitude = $answer->Latitude;
$longitude = $answer->Longitude;
$timezone = $answer->Timezone;
$gmtoffset = $answer->Gmtoffset;
$dstoffset = $answer->Dstoffset;
//Return the data as an array
return array('ip' => $ip, 'country_code' => $country_code, 'country_name' => $country_name, 'region_name' => $region_name, 'city' => $city, 'zippostalcode' => $zippostalcode, 'latitude' => $latitude, 'longitude' => $longitude, 'timezone' => $timezone, 'gmtoffset' => $gmtoffset, 'dstoffset' => $dstoffset);
}
$ip = $_SERVER['REMOTE_ADDR'];
$ip_data=locateIp($ip);
echo "IP : " . $ip_data['ip'] . "";
echo "Country code : " . $ip_data['country_code'] . "<br>";
echo "Country name : " . $ip_data['country_name'] . "<br>";
echo "Region name : " . $ip_data['region_name'] . "<br>";
echo "City : " . $ip_data['city'] . "<br>";
echo "Zip/postal code : " . $ip_data['zippostalcode'] . "<br>";
echo "Latitude : " . $ip_data['latitude'] . "<br>";
echo "Longitude : " . $ip_data['longitude'] . "<br>";
echo "Timezone : " . $ip_data['timezone'] . "<br>";
echo "GmtOffset : " . $ip_data['gmtoffset'] . "<br>";
echo "DstOffset : " . $ip_data['dstoffset'] . "<br>";
Comments
Post a Comment