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