IP 获得地址与经纬度

共计1041字,阅读大约4分钟。
<?php

//获得ip
//ip是否来自共享互联网
$ip_address = '';
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
    $ip_address = $_SERVER['HTTP_CLIENT_IP'];
}
//ip是否来自代理
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
    $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
//ip是否来自远程地址
else
{
    $ip_address = $_SERVER['REMOTE_ADDR'];
}

function baidu($ip_address){
    $url = "https://api.map.baidu.com/location/ip?ak=tbO6Qv61uwHlNmV5FqHeMlzvWnL6Ce00&ip={$ip_address}"; //HTTPS协议
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  // 对认证证书来源的检查
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);  // 获取的信息以文件流的形式返回
    curl_exec($ch);
    return $ch;
}
?>
<h1>
<?php
//发送
var_dump(baidu($ip_address));
?>
</h1>
{"address":"CN|江西|九江|None|CHINANET|0|0","content":{"address_detail":{"province":"江西省","city":"九江市","district":"","street":"","street_number":"","city_code":349},"address":"江西省九江市","point":{"y":"3446185.89","x":"12913184.52"}},"status":0}
温馨提示:本文最后更新于2022-06-13 21:12:07,某些文章具有时效性,若有错误或已失效,请在下方留言或联系雅舍站长
© 版权声明
THE END
有所帮助就支持一下吧
点赞0当赏 分享
箴言区 抢沙发
头像
达瓦里希请发言...
提交
头像

昵称

取消
昵称表情代码图片