function geoLocationByIp($ip)
{
$domain = 'http://www.netip.de/search?query=';
if(!filter_var($ip, FILTER_VALIDATE_IP))
{
throw new InvalidArgumentException("IP ?");
}
$response=@file_get_contents($domain.$ip);
if (empty($response))
{
throw new InvalidArgumentException("Erreur contact Geo-IP-Server");
}
$patterns=array();
$patterns["domain"] = '#Domain: (.*?) #i';
$patterns["country"] = '#Country: (.*?) #i';
$patterns["state"] = '#State/Region: (.*?)<br#i';
$patterns["town"] = '#City: (.*?)<br#i';
$ipInfo=array();
//check response from ipserver for above patterns
foreach ($patterns as $key => $pattern)
{
//store the result in array
$ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found';
}
// host, state, country, town
$c = utf8_encode($ipInfo['country']);
$t = utf8_encode($ipInfo['town']);
$s = utf8_encode($ipInfo['state']);
$c = str_replace('not found','?', $c);
$t = str_replace('not found','?', $t);
$s = str_replace('not found','?', $s);
$loc = $c . ' : ' . $s . ' : ' . $t;
return $loc ;
}