php未从freegeoip.net获取邮政编码



我到目前为止使用的脚本可以拉动IP,lat和long。邮政编码没有被删除。我无法弄清楚什么是破碎的以及它需要什么才能使它起作用。有什么想法吗?

// Function to get the client ip address
function getUserIP()
{
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];
    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }
    return $ip;
}
$ipaddress = getUserIP();
$geoIP  = 
json_decode(file_get_contents("http://freegeoip.net/json/$ipaddress"), true);
$lat = $geoIP['latitude'];
$lon = $geoIP['longitude'];
$zip = $geoIP['zip'];

旧的freegeoip已折旧,新服务IPSTACK提供免费的API访问。以下是一个简单的卷曲调用,它将获取您想要的信息(以及更多(。

$url = 'http://api.ipstack.com/134.201.250.155?access_key=YOURKEY';  
$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_HEADER, 0);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
$output = curl_exec($ch);  
$api_result = json_decode($output, true); 
$lat = $api_result['latitude']; 
$lon = $api_result['longitude']; 
$zip = $api_result['zip'];
echo $zip;
echo $lat;
echo $lon;

最新更新