找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 200|回复: 0

[基础] 根据手机号获取归属地

  [复制链接]
发表于 2023-2-22 21:54 | 显示全部楼层 |阅读模式
//根据手机号获取归属地
function get_province_from_mobile($mobile)
{
    //要返回的数据格式
    $ret    = [
            'mobile'   => $mobile ?: '',
            'province' => '',
            'city'     => '',
    ];
    if(empty($mobile) || strlen($mobile) != 11){
        return $ret;
    }

    //随机选择一个网址取数据
    $num      = mt_rand(1,5);
    $province = '';
    $city     = '';
    //$num=6;
    switch ($num) {
        case 1:
            //普通会员每天只能调用本接口50次
            $url  = 'https://www.iteblog.com/api/mobile.php?mobile='.$mobile;
            $html = curl_($url);
            $json = json_decode($html, true);
            if(!empty($json['province'])){
                $province = $json['province'];
                $city     = $json['city'];
            }
            break;

        case 2:
            //ip138数据不全
            $url  = 'http://m.ip138.com/mobile.asp?mobile='.$mobile;
            $html = curl_($url);
            preg_match_all('$<td>卡号归属地</td><td><span>(.*?)<$s', $html, $match);
            if(!empty($match[1][0])){
                if(stripos($match[1][0], ' ') === false){
                    $province = $city = $match[1][0];
                }else{
                    list($province, $city) = explode(' ', $match[1][0]);
                }
            }
            break;

        case 3:
            //ip138数据不全
            $url  = 'http://www.ip138.com:8080/search.asp?action=mobile&mobile='.$mobile;
            $html = curl_($url);
            //$html = mb_convert_encoding($html, 'utf-8', 'gb2312');//不知为什么转换的是乱码,弄不清楚和iconv的区别
            $html = iconv('gb2312', 'utf-8//IGNORE', $html);
            $html = preg_replace('/<!-- (.*?) -->/is', '', $html);
            $html = str_ireplace('width=*', '', $html);
            preg_match_all('#<td(.*?)>(.*?)</td>#is', $html, $match);
            if(!empty($match[2][6])){
                if(stripos($match[2][6], '&nbsp;') === false){
                    $province = $city = $match[2][6];
                }else{
                    list($province, $city) = explode('&nbsp;', $match[2][6]);
                }
            }
            break;

        case 4:
            $url  = 'https://cx.shouji.360.cn/phonearea.php?number='.$mobile;
            $html = curl_($url);
            $json = json_decode($html, true);
            if(!empty($json['data']['province'])){
                $province = $json['data']['province'];
                $city     = $json['data']['city'];
            }
            break;

        case 5:
            //百度接口
            $url    = 'http://mobsec-dianhua.baidu.com/dianhua_api/open/location?tel=' . $mobile;
            $html   = curl_($url);
            $res    = json_decode($html, true);
            if(!empty($res['response'])){
                $m = $res['response'][''.$mobile.'']['detail'];
                $province = $m['province'] ?: '';
                $city     = $m['area'][0]['city'] ?: '';
            }
            break;
    }

    //没有查到数据,使用最后的防线
    if(empty($province)   && empty($city)  ){
        file_put_contents('curl_error_'.$num.'.log', $url."\r\n"."\r\n\r\n", FILE_APPEND);
        //最牛B的百度搜索,注意,百度有可能会变更内容导致无法获取数据
        $url = 'https://www.baidu.com/s?ie=UTF-8&wd='.$mobile;
        $html = curl_($url);
        preg_match('$<div class="op_mobilephone_r c-gap-bottom-small">(.*?)</div>$s', $html, $match);
        if(empty($match[1])){
            preg_match('$<span class="op_fraudphone_addr c-gap-right-small">(.*?)</span>$s', $html, $cc);
            if(stripos($cc[1], ' ') === false){
                $province = $city = $cc[1];
            }else{
                list($province, $city) = explode(' ', $cc[1]);
            }
        }else{
            preg_match('$>(.*?)<span>(.*?)(?=&nbsp;&nbsp;)$s', $match[1], $nn);
            if(!empty($nn[2])){
                if(stripos($nn[2], '&nbsp;') === false){
                    $province = $city = $nn[2];
                }else{
                    list($province, $city) = explode('&nbsp;', $nn[2]);
                    $province = $province ?: $city;
                }
            }
        }
        /////
        file_put_contents('curl_error_last_1.log', $url."\r\n"."\r\n\r\n", FILE_APPEND);
    }
    if(empty($province) && empty($city) && $num != 4){
        $url  = 'https://cx.shouji.360.cn/phonearea.php?number='.$mobile;
        $html = curl_($url);
        $json = json_decode($html, true);
        if(!empty($json['data']['province'])){
            $province = $json['data']['province'];
            $city     = $json['data']['city'];
        }
        file_put_contents('curl_error_last_2.log', $url."\r\n"."\r\n\r\n", FILE_APPEND);
    }

    //深圳特殊一点,要注意
    if(!empty($city) && ($city == '深圳' || $city == '深圳市') ){
        $province = '深圳';
    }
    if(empty($city) && !empty($province)){
        $city = $province;//重庆、北京是无法获取到下级的区
    }

    $ret['province'] = $province;
    $ret['city']     = $city;
    $ret['url']      = $url;

    return $ret;
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|学习笔记

GMT+8, 2024-5-18 14:46 , Processed in 0.021887 second(s), 14 queries , APCu On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表