首页 > 代码库 > PHP代码获取客户端IP地址经纬度及所在城市

PHP代码获取客户端IP地址经纬度及所在城市

echo $_SERVER[‘HTTP_HOST‘];
//echo $_SERVER[‘REQUEST_URI‘];
$getIp=$_SERVER["REMOTE_ADDR"];
echo ‘IP:‘,$getIp;
echo ‘<br/>‘;
$content = file_get_contents("http://api.map.baidu.com/location/ip?ak=YWNt8VcHK7Goj1yljLlMVHnWl6ZWS26t&ip={$getIp}&coor=bd09ll");
$json = json_decode($content);
echo ‘log:‘,$json->{‘content‘}->{‘point‘}->{‘x‘};//按层级关系提取经度数据
echo ‘<br/>‘;
echo ‘lat:‘,$json->{‘content‘}->{‘point‘}->{‘y‘};//按层级关系提取纬度数据
echo ‘<br/>‘;
print $json->{‘content‘}->{‘address‘};//按层级关系提取address数
echo $json->{‘content‘}->{‘address_detail‘}->{‘city_code‘};
print_r($json);

$json输出:
stdClass Object ( [address] => CN|云南|昆明|None|CHINANET|0|0 [content] => stdClass Object ( [address] => 云南省昆明市 [address_detail] => stdClass Object ( [city] => 昆明市 [city_code] => 104 [district] => [province] => 云南省 [street] => [street_number] => ) [point] => stdClass Object ( [x] => 102.71460114 [y] => 25.04915310 ) ) [status] => 0 )

PHP代码获取客户端IP地址经纬度及所在城市