首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

免費手機號碼歸屬地查詢 API 整理與 PHP 串接範例

     編輯:PHP 綜合知識

前言

在開發分類資訊、電商、表單驗證等系統時,常需根據手機號碼顯示歸屬地(省份、運營商)。本文整理 6 家主流免費 API 介面,並提供 PHP 串接淘寶 API 的完整範例。

六大免費 API 對照表

提供方請求網址範例參數回傳格式備註
淘寶https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13800138000telJSONP穩定、無需金鑰
拍拍https://virtual.paipai.com/extinfo/GetMobileProductInfo?mobile=13800138000&amount=10000&callname=cbmobile, callname, amountJSONP需回調函數
財付通https://life.tenpay.com/cgi-bin/mobile/MobileQueryAttribution.cgi?chgmobile=13800138000chgmobileXML騰訊出品
百付寶https://www.baifubao.com/callback?cmd=1059&callback=phone&phone=13800138000phone, callback, cmdJSONP支付寶旗下
115網盤https://cz.115.com/?ct=index&ac=get_mobile_local&callback=jsonp&mobile=13800138000mobile, callbackJSONP需回調
有道https://www.youdao.com/smartresult-xml/search.s?type=mobile&q=13800138000type=mobile, qXML/JSON支援 JSONP

PHP 串接淘寶 API 範例

<?php
/**
 * 查詢手機歸屬地
 *
 * @param string $mobile 11 位手機號碼
 * @return array ['province' => '省份', 'carrier' => '運營商'] 或 false
 */
function get_mobile_area(string $mobile): ?array {
    // 基本格式驗證
    if (!preg_match('/^1[3-9]\d{9}$/', $mobile)) {
        return null;
    }

    $url = 'https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=' . $mobile . '&t=' . time();
    
    // 使用 cURL 而非 file_get_contents,可控制超時與錯誤
    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT        => 5,
        CURLOPT_CONNECTTIMEOUT => 3,
    ]);
    $content = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($http_code !== 200 || empty($content)) {
        return null;
    }

    // 回傳格式:__GetZoneResult_ = {mts:'1380013', province:'北京', catName:'中國移動', ...};
    // 使用正則提取 JSON 物件
    if (preg_match('/\{.*\}/', $content, $matches)) {
        $data = json_decode($matches[0], true);
        if (json_last_error() === JSON_ERROR_NONE) {
            return [
                'province' => $data['province'] ?? '',
                'carrier'  => $data['catName'] ?? '',
            ];
        }
    }

    return null;
}

// 使用範例
$mobile = '13800138000';
$result = get_mobile_area($mobile);
if ($result) {
    echo "省份:{$result['province']}\n";
    echo "運營商:{$result['carrier']}\n";
} else {
    echo "查詢失敗或號碼格式錯誤\n";
}

進階建議:建立多 API 容錯機制

class MobileLocator {
    private $apis = [
        'taobao' => 'https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=%s',
        'youdao' => 'https://www.youdao.com/smartresult-xml/search.s?type=mobile&q=%s',
        // 可繼續加入其他 API
    ];

    public function query(string $mobile): ?array {
        foreach ($this->apis as $name => $url) {
            $result = $this->call_api($name, sprintf($url, $mobile));
            if ($result) return $result;
        }
        return null; // 全部失敗
    }

    private function call_api(string $name, string $url): ?array {
        // 實作各 API 專屬解析邏輯...
    }
}

注意事項

  • 免費 API 可能有頻率限制、隨時下線風險,生產環境建議採購商業服務或自建號段資料庫
  • 號段資料會隨運營商新增號碼段而更新,需定期同步
  • 虛擬運營商(170/171 等)歸屬地準確度較低
  • 請遵守各服務商的使用條款,勿濫用
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved