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

PHP实现手机归属地查询

2025/8/7 15:45:52发布20次查看
通过在慕课网上面的学习,完成了php实现手机归属地查询的功能。
后台 mobilequery类调用:
\libs\httprquest \libs\imredis (未使用) api类调用mobilequery类的query方法来返回$response的数据,并进行加工。
前台 base.js 使用.ajax请求,并将请求后的数据填充到index.html中。
核心代码展示 autoload.php 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 $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 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面向对象的编写有了新的理解,同时js的编写能力需要加强。
代码下载 https://github.com/cizel/trymybest/tree/master/queryphone
以上
该用户其它信息

VIP推荐

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