PHP provides detailed instructions on how to query the mobile phone home location and mobile phone ownership
This article describes how to query the mobile phone home location using PHP. First, let's take a look at the class diagram of the mobile phone home location query:
Background
MobileQuery class call:
- \ Libs \ HttpRquest
- \ Libs \ ImRedis (not used)
The Api class calls the MobileQuery Query method to return $ response data and process it.
Front-end
Base. js uses a. ajax request and fills the data behind the request into index.html.
Core code Display
AutoLoad. php
<? Php/*** TODO Auto-generated comment. */class AutoLoad {/*** TODO Auto-generated comment. */public static function load ($ className) {// Replace $ 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 = '2016 ';} $ this-> queryResponse ($ this-> phone);} public function queryResponse ($ phone) {$ this-> response = MobileQuery: query ($ phone ); if (is_array ($ this-> response) and isset ($ this-> response ['vince ']) {$ this-> response ['phone'] = $ phone; $ this-> response ['code'] = 200;} else {$ this-> response ['phone'] = $ phone; $ this-> response ['msg '] = 'mobile phone number error';} 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 = Htt PRequest: request (self: PHONE_API, ['tel '=> $ phone]); $ phoneData = self: formatData ($ response ); $ phoneData ['msg '] = 'data provided by Cizel's blog';} 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: 16:53:52 * @ Last Modified by: Cizel * @ Last Modified time: 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 ('request exception ');}});};
Perception
By completing this example, I feel that I have a new understanding of PHP object-oriented programming. At the same time, the JavaScript writing capability needs to be enhanced.
Well, the above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, you can leave a message, thank you for your support.