通过淘宝IP库获取定位

http://ip.aliyun.com/outGetIpInfo?accessKey=alibaba-inc&ip=113.108.182.52

//获取ip地址
function get_ip(){
    if (isset($_SERVER)) {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $realip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $realip = $_SERVER['HTTP_CLIENT_IP'];
        } else {
            $realip = $_SERVER['REMOTE_ADDR'];
        }
    } else {
        if (getenv("HTTP_X_FORWARDED_FOR")) {
            $realip = getenv( "HTTP_X_FORWARDED_FOR");
        } elseif (getenv("HTTP_CLIENT_IP")) {
            $realip = getenv("HTTP_CLIENT_IP");
        } else {
            $realip = getenv("REMOTE_ADDR");
        }
    }
    return $realip;
}
 
   //淘宝库ip接口
    public function get_ip()
    {
        $ip = get_ip();
        $url = file_get_contents("http://ip.aliyun.com/outGetIpInfo?accessKey=alibaba-inc&ip=$ip");
        $res1 = json_decode($url,true);
        $data =$res1;
        if ($data) {
            $this->response(array("status" => 1, "msg" => "查询成功", "result" => $data), 'json');
        } else {
            $this->response(array("status" => -1, "msg" => "查询失败"), 'json');
        }
    }

获取打印的数据

{
code: 0
data: {
 area: ""
 area_id: ""
 city: "广州"
 city_id: "440100"
 country: "中国"
 country_id: "CN"
 county: ""
 county_id: null
 ip: "113.108.182.52"
 isp: "电信"
 isp_id: "100017"
 queryIp: "113.108.182.52"
 region: "广东"
 region_id: "440000"
 msg: "query success"
 }
}

 

posted @ 2021-03-11 14:54  Mr、桔子  阅读(387)  评论(0编辑  收藏  举报