PHP实现查询手机归属地的方法详解
程序员文章站
2024-03-12 22:24:38
本文介绍的是php实现查询手机归属地的方法,首先来看看手机归属地查询的类图:
后台
mobilequery类调用:
\libs\httprquest...
本文介绍的是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 面向对象的编写有了新的理解,同时 javascript 的编写能力需要加强。
好了,以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
上一篇: JAVA 实现二叉树(链式存储结构)
下一篇: java明文密码三重加密方法