美文网首页
php批量获取手机归属地!

php批量获取手机归属地!

作者: DragonersLi | 来源:发表于2019-10-13 22:52 被阅读0次
    image.png
    
        public function index()
        {
            set_time_limit(0);
            ob_end_clean();
            ob_implicit_flush(1);
            ini_set('memory_limit','100M');
            $data = $this->shareModel->field('recom_mobile,recom_name,product_name')->select();
            foreach($data as $k=>$v){
                $location = $this->getLocation($v->recom_mobile);
                $gsd = empty($location['data']['fullname'])?'':$location['data']['fullname'];
                $res[] = ['username'=>$v->recom_name,'mobile'=>$v->recom_mobile,'productName'=>$v->product_name,'location'=>$gsd];
    
    
            }
           echo Db::table('pjh_test')->insert($res);
          #  var_dump( $this->getLocation('18858287938') );exit();
    
        }
    
        /**
         * 手机号码归属地查询
         * @param $tel
         * @return string
         */
        function getLocation($tel)
        {
            // 过滤参数
            if ( !$this->isPhoneNumber($tel) ) return ['code'=>200,'status'=>false,'msg'=>'Cell phone number error!'];
    
            // 请求地址
            $url = 'http://mobsec-dianhua.baidu.com/dianhua_api/open/location?tel='.$tel;
    
            // 发起请求
            //$res = file_get_contents($url);
            $res = $this->curlRequest($url,'','GET');
    
            if ( $res['code'] !== 200 ) return ['code'=>$res['code'],'status'=>false,'msg'=>$res['responseHeader']['msg']];// 判断请求是否成功
    
            $data = $res['response'][$tel];// 接收返回值
    
            if ( !$data ) return ['code'=>200,'status'=>false,'msg'=>'API Exception!'];// 返回值为空
    
            $response['province'] = $data['detail']['province'];        // 归属地
            $response['city'] = $data['detail']['area'][0]['city'];   // 城市
            $response['service'] = $data['detail']['operator'];     // 运行商
            $response['fullname'] = $data['location'];            // 运行商全称
    
            return ['code'=>200,'status'=>true,'data'=>$response];
        }
    
        /**
         * 手机号码格式验证
         * @param $tel
         * @return bool
         */
        function isPhoneNumber($tel)//手机号码正则表达试
        {
            return (preg_match("/0?(13|14|15|17|18|19)[0-9]{9}/",$tel))?true:false;
        }
    
        /**
         * 发起CURL请求
         * @param string $url 请求地址
         * @param string $data 请求数据
         * @param string $method 请求方式
         * @return array 一维数组
         */
        function curlRequest($url,$data = '',$method = 'POST')
        {
            $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, $method); //设置请求方式
    
            curl_setopt($ch,CURLOPT_HTTPHEADER,array("X-HTTP-Method-Override: $method"));//设置HTTP头信息
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串
            $document = curl_exec($ch);//执行预定义的CURL
            $code = curl_getinfo($ch,CURLINFO_HTTP_CODE); //获取HTTP请求状态码~
            curl_close($ch);
    
            $document = json_decode($this->removeBOM($document),true);
            $document['code'] = $code;
    
            return $document;
        }
    
        /**
         * 检测并移除 BOM 头
         * @param string $str 字符串
         * @return string 去除BOM以后的字符串
         */
        function removeBOM($str = '')
        {
            if (substr($str, 0,3) == pack("CCC",0xef,0xbb,0xbf)) {
                $str = substr($str, 3);
            }
            return $str;
        }
    
    

    相关文章

      网友评论

          本文标题:php批量获取手机归属地!

          本文链接:https://www.haomeiwen.com/subject/xaxaectx.html