PHP手機號碼歸屬地查詢代碼(API接口/mysql)
更新時間:2012年09月04日 23:14:09 作者:
本文來介紹一下關(guān)于手機號碼歸屬地實現(xiàn)方法,我們可以利用api接口與mysql+php來實例有需要的同學(xué)看看
首先我們介紹使用自己的數(shù)據(jù)庫查詢多個手機號碼,那還是建議你擁有一個自己的的手機號碼數(shù)據(jù)庫。正常情況下,只是滿足一般查詢的話,你不需要去購買專業(yè)版的手機號碼數(shù)據(jù)庫,增加無謂成本。我免費為你提供一個ACCESS數(shù)據(jù)庫,包含17萬多條數(shù)據(jù),常用的130-139、150-159以及180-189開頭手機號碼段都在其中,你可以借助數(shù)據(jù)庫工具輕松地將它轉(zhuǎn)換成MYSQL或其它版本數(shù)據(jù)庫
最新手機號碼數(shù)據(jù)庫下載地址:http://xiazai.jb51.net/201209/yuanma/phone-number-database-jb51.rar
PHP+MYSQL手機號碼歸屬地查詢實現(xiàn)方法
通過上面的介紹,我們已經(jīng)有了自己的MYSQL數(shù)據(jù)表。這個表結(jié)構(gòu)很簡單:ID(序號),code(區(qū)號),num(手機號碼段),cardtype(手機卡類型),city(手機號碼歸屬地)。注意,這個表存儲數(shù)據(jù)量很大,應(yīng)當根據(jù)你的sql查詢語句,建立合適的索引字段,以提高查詢效率。
1)獲取手機號碼歸屬地,我們只需要通過判斷手機號碼段歸屬地即可。主要通過以下函數(shù)實現(xiàn),其中GetAlabNum、cn_substr、str_replace都是字符串操作函數(shù),$dsql是數(shù)據(jù)庫操作類。
function GetTelphone($tel)
{
global $city,$dsql;
if(isset($tel)) $tel = GetAlabNum(trim($tel));//GetAlabNum函數(shù)用于替換全角數(shù)字,將可能存在的非法手機號碼轉(zhuǎn)換為數(shù)字;trim去除多余空格。
else return false;
if(strlen($tel) < 7) return false;
$tel = cn_substr($tel, 11);//先截取11個字符,防止是多個手機號碼
//if(!is_numeric($tel)) return false;
if(cn_substr($tel, 1) == "0")//判斷手機號碼是否以0開頭,這種情況可能會是座機號以0開頭
{
if(cn_substr($tel, 2) == "01" || cn_substr($tel, 2) == "02") $tel = cn_substr($tel, 3);//3位區(qū)號
else $tel = cn_substr($tel, 4);
$row = $dsql->GetOne(" Select code,city as dd from `#@__tel` where code='$tel' group by code ");
}
else
{
$tel = cn_substr($tel, 7);
$row = $dsql->GetOne(" Select num,city as dd from `#@__tel` where num='$tel' ");
}
$city = $row['dd'];
if($city)
{
$city = str_replace("省", "-", $city);
$city = str_replace("市", "", $city);
$city = "<br /><font color="green">[".$city."]</font>";
return $city;
}
}
api實現(xiàn)方法,這里不需要自己的數(shù)據(jù)庫但有限制了
主要使用curl實現(xiàn),需要開啟php對curl的支持。
<?php
header(“Content-Type:text/html;charset=utf-8″);
if (isset($_GET['number'])) {
$url = ‘http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo';
$number = $_GET['number'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, “mobileCode={$number}&userId=”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$data = simplexml_load_string($data);
if (strpos($data, ‘http://')) {
echo ‘手機號碼格式錯誤!';
} else {
echo $data;
}
}
?>
<form action=”mobile.php” method=”get”>
手機號碼: <input type=”text” name=”number” /> <input type=”submit” value=”提交” />
</form>
與php mysql手機號碼歸屬地查詢這個會慢很多,畢竟要通過第三方法數(shù)據(jù)。
最新手機號碼數(shù)據(jù)庫下載地址:http://xiazai.jb51.net/201209/yuanma/phone-number-database-jb51.rar
PHP+MYSQL手機號碼歸屬地查詢實現(xiàn)方法
通過上面的介紹,我們已經(jīng)有了自己的MYSQL數(shù)據(jù)表。這個表結(jié)構(gòu)很簡單:ID(序號),code(區(qū)號),num(手機號碼段),cardtype(手機卡類型),city(手機號碼歸屬地)。注意,這個表存儲數(shù)據(jù)量很大,應(yīng)當根據(jù)你的sql查詢語句,建立合適的索引字段,以提高查詢效率。
1)獲取手機號碼歸屬地,我們只需要通過判斷手機號碼段歸屬地即可。主要通過以下函數(shù)實現(xiàn),其中GetAlabNum、cn_substr、str_replace都是字符串操作函數(shù),$dsql是數(shù)據(jù)庫操作類。
復(fù)制代碼 代碼如下:
function GetTelphone($tel)
{
global $city,$dsql;
if(isset($tel)) $tel = GetAlabNum(trim($tel));//GetAlabNum函數(shù)用于替換全角數(shù)字,將可能存在的非法手機號碼轉(zhuǎn)換為數(shù)字;trim去除多余空格。
else return false;
if(strlen($tel) < 7) return false;
$tel = cn_substr($tel, 11);//先截取11個字符,防止是多個手機號碼
//if(!is_numeric($tel)) return false;
if(cn_substr($tel, 1) == "0")//判斷手機號碼是否以0開頭,這種情況可能會是座機號以0開頭
{
if(cn_substr($tel, 2) == "01" || cn_substr($tel, 2) == "02") $tel = cn_substr($tel, 3);//3位區(qū)號
else $tel = cn_substr($tel, 4);
$row = $dsql->GetOne(" Select code,city as dd from `#@__tel` where code='$tel' group by code ");
}
else
{
$tel = cn_substr($tel, 7);
$row = $dsql->GetOne(" Select num,city as dd from `#@__tel` where num='$tel' ");
}
$city = $row['dd'];
if($city)
{
$city = str_replace("省", "-", $city);
$city = str_replace("市", "", $city);
$city = "<br /><font color="green">[".$city."]</font>";
return $city;
}
}
api實現(xiàn)方法,這里不需要自己的數(shù)據(jù)庫但有限制了
主要使用curl實現(xiàn),需要開啟php對curl的支持。
復(fù)制代碼 代碼如下:
<?php
header(“Content-Type:text/html;charset=utf-8″);
if (isset($_GET['number'])) {
$url = ‘http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo';
$number = $_GET['number'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, “mobileCode={$number}&userId=”);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$data = simplexml_load_string($data);
if (strpos($data, ‘http://')) {
echo ‘手機號碼格式錯誤!';
} else {
echo $data;
}
}
?>
<form action=”mobile.php” method=”get”>
手機號碼: <input type=”text” name=”number” /> <input type=”submit” value=”提交” />
</form>
與php mysql手機號碼歸屬地查詢這個會慢很多,畢竟要通過第三方法數(shù)據(jù)。
相關(guān)文章
php array_filter除去數(shù)組中的空字符元素
php array_filter除去數(shù)組中的空字符元素,array_filter() 函數(shù)根據(jù)回調(diào)函數(shù)過濾數(shù)組中的值,省略回調(diào)函數(shù)則默認過濾空值,需要的朋友可以參考下。2011-11-11PHP實現(xiàn)繪制二叉樹圖形顯示功能詳解【包括二叉搜索樹、平衡樹及紅黑樹】
這篇文章主要介紹了PHP實現(xiàn)繪制二叉樹圖形顯示功能,結(jié)合實例形式分析了php繪制常見二叉樹的相關(guān)操作技巧,包括二叉搜索樹、平衡樹及紅黑樹的實現(xiàn)方法,需要的朋友可以參考下2017-11-11使用session判斷用戶登錄用戶權(quán)限(超簡單)
本篇文章是對session判斷用戶登錄用戶權(quán)限進行了詳細的分析介紹,需要的朋友參考下2013-06-06php提示Failed to write session data錯誤的解決方法
這篇文章主要介紹了php提示Failed to write session data錯誤的解決方法,較為詳細的分析了session寫入錯誤的原因與解決方法,并附帶說明了php的工作機制,非常具有實用價值,需要的朋友可以參考下2014-12-12