vue 中获取客户端所在ip及ip所属的地理位置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vue 中获取客户端所在ip及ip所属的地理位置相关的知识,希望对你有一定的参考价值。

参考技术A 在index.html中引入

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 )

















以上是关于vue 中获取客户端所在ip及ip所属的地理位置的主要内容,如果未能解决你的问题,请参考以下文章

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

Vue利用搜狐获取公网ip地址

Vue实战041:获取当前客户端IP地址详解(内网和外网)

php 怎么通过ip来获取所在位置

php获取ip地址所在的地理位置的实现

GEOIP 并获取计算机 IP 地址 位置?