您的位置:首页 > 编程语言 > PHP开发

php根据手机号码获取归属地|所在地区 

2018-03-02 09:16 459 查看
/*** 根据手机号码获取归属地*/function getMobileLoc($mobile){    $data = array('province'=>'','city'=>'');    //初始化变量    //根据百度的数据库调用返回值    header("Content-type:text/html;charset=utf-8");    $url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={$mobile}&resource_id=6004&ie=utf8&oe=utf8&format=json";    $result = curl_https($url);    $result = (array)json_decode($result);    if (!empty($result['data'])) {        $result = (array)$result['data'][0];        $data['city'] = empty($result['city']) ? '' : $result['city'];        $data['province'] = empty($result['prov']) ? $sms['city'] : $result['prov'];    }    return $data;
    /*    $url = "http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel={$mobile}&t=".time();    preg_match_all("/(\w+):'([^']+)/", $content, $matches);    $data = array_combine($matches[1], $matches[2]);    print_r($data);     exit;    */       }
/** * curl请求 */function curl_https($url){    $ch = curl_init();    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);    curl_setopt($ch, CURLOPT_HEADER, FALSE);    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);    curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_REFERER, $url);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);    $result = curl_exec($ch);    curl_close($ch);
    //-------请求为空    if(empty($result)){        return false;    }        return $result;} 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: