结合前两篇手机号获取用户城市所在地再次升级,增加通过客户端ip获取用户所在地

续前两篇通过手机号码获取用户归属地后
为确保程序稳定高效运行,在此增加通过客户端ip获取用户所在地
(通过手机号码,只能获取用户手机号注册城市,如程序关于地点有明确要求,请删减使用)

/**
     * 手机号获取城市
     * 2018-06-21 Angus
     * zhn6@sina.cn
     * */
    public function PhoneCity($Mobile){
        $City = array();
        $Url = 'http://mobsec-dianhua.baidu.com/dianhua_api/open/location?tel='.$Mobile;
        $result = json_decode($this ->getCurlRequest($Url), true);
        if(isset($result['response'][$Mobile]['detail']['area'][0]['city'])){
            $City['cityname'] = $result['response'][$Mobile]['detail']['area'][0]['city'];
            $City['province'] = $result['response'][$Mobile]['detail']['province'];
        }else{
            $Url = 'https://www.so.com/s?q='.$Mobile.'&src=hao_tool';
            $result = $this ->postCurlRequest($Url);
            preg_match('/<p class=\"mh-detail\">([^<]*)<\/p>/is',$result, $re);
            $qian=array("&nbsp;","\t","\r","\n");
            $hou=array("","","","|");
            $str = str_replace($qian,$hou,$re[1]);
            $CityArray = explode('|', $str);
            $City['cityname'] = empty($CityArray[4])?$CityArray[3]:$CityArray[4];
            $City['province'] = $CityArray[3];
        }
        $City['cityid'] = M('Area') ->where(array('name'=>array('like', $City['cityname']."%"), 'level'=>2)) ->getField('zone_id');
        return $City;
    }

    /**
     * Ip获取城市
     * */
    public function IpCity(){
        $ip = @file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".get_client_ip());
        $ip = json_decode($ip,true);
        if($ip['code'] != 0){return false;}
        if($ip['data']['city_id'] == 'xx' || $ip['data']['region'] == 'XX' || $ip['data']['city_id'] == 'local'){return false;}
        return array('cityname'=>$ip['data']['city'], 'cityid'=>$ip['data']['city_id'], 'province'=>$ip['data']['region']);
    }

    function getCurlRequest($Url){
        $ch = curl_init(); //初始化CURL句柄
        curl_setopt($ch, CURLOPT_URL, $Url); //设置请求的URL
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串,而s不是直接输出
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); //设置请求方式
        curl_setopt($ch,CURLOPT_HTTPHEADER,array("X-HTTP-Method-Override: GET"));//设置HTTP头信息
        $document = curl_exec($ch);//执行预定义的CURL
        curl_close($ch);
        return $document;
    }

    function postCurlRequest($Url){
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $Url );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ( $ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, array (
            'Content-Type: application/json'
        ));
        $result = curl_exec ( $ch );
        curl_close ( $ch );
        return $result;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值