您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP查询手机归属地的方法详解

2026/1/13 2:09:57发布27次查看
最近通过在网上面的学习,完成了 php 实现手机归属地查询的功能。所以下面这篇文章主要给大家介绍了利用php实现查询手机归属地的方法,文中给出了详细的代码示例,需要的朋友可以参考借鉴,下面来一起看看吧。
本文介绍的是php实现查询手机归属地的方法,首先来看看手机归属地查询的类图:
后台
mobilequery类调用:
\libs\httprquest
\libs\imredis (未使用)
api类调用 mobilequery 类的 query 方法来返回 $response 的数据,并进行加工。
前台
base.js 使用 .ajax 请求,并将请求后的数据填充到index.html 中。
核心代码展示
autoload.php
<?php /** * todo auto-generated comment. */ class autoload { /** * todo auto-generated comment. */ public static function load($classname) { //命名空间的反斜杠替代 $filename = sprintf('%s.php',str_replace('\\','/',$classname)); require_once($filename); } } spl_autoload_register(['autoload','load']);
api.php
<?php /** * todo auto-generated comment. */ require_once "autoload.php"; use \app\mobilequery; class api{ private $response; private $params; private $phone; public function __construct(){ $this->params = $_post; if(isset($this->params['tel'])) { $this->phone = $this->params['tel']; }else { $this->phone = '17706436202'; } $this->queryresponse($this->phone); } public function queryresponse($phone) { $this->response = mobilequery::query($phone); if(is_array($this->response) and isset($this->response['province'])) { $this->response['phone'] = $phone; $this->response['code'] = 200; } else { $this->response['phone'] = $phone; $this->response['msg'] = '手机号码错误'; } } public function getresponse() { return json_encode($this->response); } } $api = new api(); echo $api->getresponse();
app/mobilequery.php
<?php /** * todo auto-generated comment. */ namespace app; use libs\httprequest; use libs\imredis; class mobilequery { /** * todo auto-generated comment. */ const phone_api= 'https://tcc.taobao.com/cc/json/mobile_tel_segment.htm'; /** * todo auto-generated comment. */ const query_phone = 'phone:info:'; public static function query($phone) { $phonedata = null; if(self::varifyphone($phone)){ $response = httprequest::request(self::phone_api, ['tel'=>$phone]); $phonedata = self::formatdata($response); $phonedata['msg'] ='数据由cizel的博客提供'; } return $phonedata; } public static function formatdata($data) { $ret = null; if(!empty($data)){ preg_match_all("/(\w+):'([^']+)/",$data,$res); $items = array_combine($res[1],$res[2]); foreach ($items as $itemkey => $itemval) { $ret[$itemkey] = iconv('gb2312','utf-8',$itemval); } } return $ret; } public static function varifyphone($phone) { if(preg_match("/^1[34578]{1}\d{9}/",$phone)) { return true; } else { return false; } } }
lib/httprequest.php
<?php /** * todo auto-generated comment. */ namespace libs; class httprequest { /** * todo auto-generated comment. */ public static function request($url, $params =[], $method='get') { $ret = null; if(preg_match("/^(http|https)\:\/\/(\w+\.\w+\.\w+)/",$url)) { $method = strtoupper($method); if($method == 'post') { exit('nothing to do.'); } else { if($params) { if(strripos('?',$url)) { $url = $url . '&' . http_build_query($params); } else { $url = $url . '?' . http_build_query($params); } } $ret = file_get_contents($url); } } return $ret; } }
statis/js/base.js
/* * @author: cizel * @date: 2016-04-11 16:53:52 * @last modified by: cizel * @last modified time: 2016-04-11 17:22:32 */ $(document).ready(function(){ $('#query').click(function(){ var phone = $('#phone_num').val(); //alert(phone); if (phone.length == 11) { cizel.global.ajax('api.php','post',{'tel':phone},'json',cizel.apps.queryphone.ajaxcallback); } }); }); var cizel = cizel || {}; cizel.global = {}; cizel.apps = {}; cizel.apps.queryphone = {}; cizel.apps.queryphone.ajaxcallback = function(data){ if(data.code == 200){ cizel.apps.queryphone.showinfo(); $('#phonenumber').text(data.phone); $('#phoneprovince').text(data.province); $('#phonecatname').text(data.catname); $('#phonemsg').text(data.msg); } else { cizel.apps.queryphone.hideinfo(); } }; cizel.apps.queryphone.showinfo = function(){ $('#phoneinfo').show(); } cizel.apps.queryphone.hideinfo = function(){ $('#phoneinfo').hide(); } cizel.global.ajax = function(url, method, params,datatype,callback) { $.ajax({ url: url, type: method, data: params, datatype: datatype, success:callback, error:function(){ alert('请求异常'); } }); };
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
php观察者模式应用场景实例详解_php技巧
php水印类,支持添加图片、文字、填充颜色区域的实现_php实例
php读取sqlite数据库入门实例代码_javascript技巧
以上就是php查询手机归属地的方法详解的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product