到目前为止,我编写的脚本可以提取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'];发布于 2018-04-26 01:44:38
旧的freegeoip已经贬值,新的服务ip堆栈提供了免费的api访问。下面是一个简单的curl调用,它将获得您想要的信息(还有更多)。
$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;https://stackoverflow.com/questions/50033560
复制相似问题