PHP手机号码归属地查询代码(API接口/mysql)

时间:2023-10-26 04:35:44 

首先我们介绍使用自己的数据库查询多个手机号码,那还是建议你拥有一个自己的的手机号码数据库。正常情况下,只是满足一般查询的话,你不需要去购买专业版的手机号码数据库,增加无谓成本。我免费为你提供一个ACCESS数据库,包含17万多条数据,常用的130-139、150-159以及180-189开头手机号码段都在其中,你可以借助数据库工具轻松地将它转换成MYSQL或其它版本数据库
最新手机号码数据库下载地址:http://xiazai.aspxhome.com/201209/yuanma/phone-number-database-jb51.rar
PHP+MYSQL手机号码归属地查询实现方法
通过上面的介绍,我们已经有了自己的MYSQL数据表。这个表结构很简单:ID(序号),code(区号),num(手机号码段),cardtype(手机卡类型),city(手机号码归属地)。注意,这个表存储数据量很大,应当根据你的sql查询语句,建立合适的索引字段,以提高查询效率。
1)获取手机号码归属地,我们只需要通过判断手机号码段归属地即可。主要通过以下函数实现,其中GetAlabNum、cn_substr、str_replace都是字符串操作函数,$dsql是数据库操作类。


function GetTelphone($tel)
{
global $city,$dsql;
if(isset($tel)) $tel = GetAlabNum(trim($tel));//GetAlabNum函数用于替换全角数字,将可能存在的非法手机号码转换为数字;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位区号
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实现方法,这里不需要自己的数据库但有限制了
主要使用curl实现,需要开启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手机号码归属地查询这个会慢很多,毕竟要通过第三方法数据。

标签:手机号码,归属地
0
投稿

猜你喜欢

  • Python实现从多表格中随机抽取数据

    2022-07-01 01:58:18
  • golang中json反序列化可能遇到的问题

    2024-04-27 15:36:12
  • 小议javascript设计模式

    2009-10-09 13:31:00
  • 在Python编程过程中用单元测试法调试代码的介绍

    2023-12-10 02:16:46
  • 对python3.4 字符串转16进制的实例详解

    2022-03-29 16:15:17
  • Vue中的baseurl如何配置

    2024-05-09 15:10:46
  • Python中输出ASCII大文字、艺术字、字符字小技巧

    2021-03-03 00:06:49
  • 实现用python算法计算圆周率的小诀窍

    2023-10-16 15:16:27
  • JavaScript编写棋盘覆盖代码详解

    2024-04-17 10:30:05
  • Python获取当前脚本文件夹(Script)的绝对路径方法代码

    2021-05-27 09:57:52
  • Vue实现web分页组件详解

    2024-04-30 10:39:27
  • MySQL 行锁和表锁的含义及区别详解

    2024-01-23 10:06:11
  • Python 多进程并发操作中进程池Pool的实例

    2022-06-28 16:31:37
  • Python面向对象类的继承实例详解

    2023-04-19 11:06:38
  • Python urllib.request对象案例解析

    2022-04-14 22:24:43
  • python 提高开发效率的5个小技巧

    2022-05-11 00:55:09
  • Django后台admin的使用详解

    2023-11-21 14:57:50
  • Python第三方常用模块openpyxl的简单介绍

    2021-05-28 16:10:09
  • Python word实现读取及导出代码解析

    2021-05-11 07:56:47
  • 聊聊golang的defer的使用

    2023-07-21 13:15:02
  • asp之家 网络编程 m.aspxhome.com