在開發分類資訊、電商、表單驗證等系統時,常需根據手機號碼顯示歸屬地(省份、運營商)。本文整理 6 家主流免費 API 介面,並提供 PHP 串接淘寶 API 的完整範例。
| 提供方 | 請求網址範例 | 參數 | 回傳格式 | 備註 |
|---|---|---|---|---|
| 淘寶 | https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13800138000 | tel | JSONP | 穩定、無需金鑰 |
| 拍拍 | https://virtual.paipai.com/extinfo/GetMobileProductInfo?mobile=13800138000&amount=10000&callname=cb | mobile, callname, amount | JSONP | 需回調函數 |
| 財付通 | https://life.tenpay.com/cgi-bin/mobile/MobileQueryAttribution.cgi?chgmobile=13800138000 | chgmobile | XML | 騰訊出品 |
| 百付寶 | https://www.baifubao.com/callback?cmd=1059&callback=phone&phone=13800138000 | phone, callback, cmd | JSONP | 支付寶旗下 |
| 115網盤 | https://cz.115.com/?ct=index&ac=get_mobile_local&callback=jsonp&mobile=13800138000 | mobile, callback | JSONP | 需回調 |
| 有道 | https://www.youdao.com/smartresult-xml/search.s?type=mobile&q=13800138000 | type=mobile, q | XML/JSON | 支援 JSONP |
<?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";
}
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 專屬解析邏輯...
}
}