调用外部接口获取用户ip地址

    //通过搜狐接口获取用户的ip;
        $json = file_get_contents('http://pv.sohu.com/cityjson?ie=utf-8');
        preg_match_all('/"cip"\s*:\s*"([^"]+)"/', $json, $result, PREG_SET_ORDER);
        if (empty($result)) {
            $this->error("未获取到用户ip!");
        }
        $ip = $result[0][1];
        //调用淘宝接口获取城市;
        $res = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$ip");
        $res = json_decode($res, true);
        //获取浏览用户所在城市;
        $m   = $res['data']['city'].'市';

  

posted @ 2018-11-21 10:18  王芬老师  阅读(1241)  评论(0编辑  收藏  举报