第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

PHP實現(xiàn)手機歸屬地查詢

帥進超 軟件工程師
難度中級
時長 1小時10分
學習人數(shù)
綜合評分9.57
68人評價 查看評價
9.7 內(nèi)容實用
9.4 簡潔易懂
9.6 邏輯清晰
  • index.html <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>手機歸屬地查詢</title> <script src="static/js/jquery.min.js"></script> <script src="static/js/base.js"></script> <link rel="stylesheet" href="static/css/bootstrap.min.css"/> <link rel="stylesheet" href="static/css/flat-ui.css"/> <style> body {max-width: 720px;margin: 0 auto;} .top-20 {margin-top: 20px;} .table-width {margin-left: 20px;width: 95%;} #info {display: none;} </style> </head> <body> <div class="row top-20"> <div class="col-xs-9"> <input type="text" id="phone-num" class="form-control"/> </div> <div class="col-xs-3"> <button class="btn btn-lg btn-block btn-primary" id="query">查詢</button> </div> </div> <div class="row top-20" id="info"> <table class="table table-width table-responsive"> <tr><th>手機號碼</th><td id="query-tel">15933643352</td></tr> <tr><th>歸屬地</th><td id="query-pro">北京</td></tr> <tr><th>運營商</th><td id="query-cat">中國移動</td></tr> <tr><th>其他</th><td id="query-other">慕課網(wǎng)</td></tr> </table>
    查看全部
  • 如果通過
    查看全部
  • QueryPhone.php <?php namespace app; use libs\ImHttpRequest; use libs\ImRedis; class QueryPhone{ //定義hash的key const CACHE_KEY = 'PHONE:INFO'; public static function query($phone){ $ret = []; if (self::verifyPhone($phone)){ $redisKey = substr($phone,0,7); $phoneInfo = ImRedis::getRedis()->hGet(self::CACHE_KEY,$redisKey);//從數(shù)據(jù)庫中讀取數(shù)據(jù) if ($phoneInfo) { $ret = json_decode($phoneInfo,true);//如果$phoneInfo為真就轉化為數(shù)組,true要寫上,不然會轉化為對象 $ret['msg'] = '由慕課網(wǎng)提供數(shù)據(jù)'; }else{ $response = ImHttpRequest::request(self::TAOBAO_API,['tel'=>$phone]); $data = self::formatData($response); if ($data) { $json = json_encode($data);//把數(shù)組轉化為JSON格式 ImRedis::getRedis()->hSet(self::CACHE_KEY,$redisKey,$json);//把數(shù)據(jù)寫到數(shù)據(jù)庫中 $data['msg'] = '由阿里巴巴提供數(shù)據(jù)'; $ret = $data; } } } return $ret; } } api.php <?php require_once "autoload.php"; $info = app\QueryPhone::query('13987654321'); var_dump($info); 刷新瀏覽器 (從數(shù)據(jù)庫中讀取數(shù)據(jù)) msg = 由慕課網(wǎng)提供數(shù)據(jù) 修改api.php里的電話號碼 $info = app\QueryPhone::query('13623323388'); 第一次刷新瀏覽器 (從淘寶API獲取數(shù)據(jù)) msg = 由阿里巴巴提供數(shù)據(jù)
    查看全部
  • QueryPhone.php <?php namespace app; use libs\ImHttpRequest; use libs\ImRedis; class QueryPhone{ //定義hash的key const CACHE_KEY = 'PHONE:INFO'; public static function query($phone){ if (self::verifyPhone($phone)){ $redisKey = substr($phone,0,7); $response = ImHttpRequest::request(self::TAOBAO_API,['tel'=>$phone]); $data = self::formatData($response); if ($data) { $json = json_encode($data);//把數(shù)組轉化為JSON格式 ImRedis::getRedis()->hSet(self::CACHE_KEY,$redisKey,$json);//把數(shù)據(jù)寫到數(shù)據(jù)庫中 } } } } 刷新瀏覽器 cmd keys * "PHONE:INFO" 這個key已經(jīng)寫進去了 hgetall PHONE:INFO 1) "1398765" 號段的信息也已寫進去
    查看全部
  • QueryPhone.php <?php namespace app; use libs\ImHttpRequest; use libs\ImRedis; class QueryPhone{ //定義hash的key const CACHE_KEY = 'PHONE:INFO'; public static function query($phone){ if (self::verifyPhone($phone)){ $redisKey = substr($phone,0,7); $response = ImHttpRequest::request(self::TAOBAO_API,['tel'=>$phone]); $data = self::formatData($response); if ($data) { $json = json_encode($data);//把數(shù)組轉化為JSON格式 //var_dump($json);//打印出來返回false[編碼問題,需要把GBK轉換為Unicode] } } } public static function formatData($data = null){ $ret = false; if ($data) { preg_match_all("/(\w+):'([^']+)/",$data,$res);//用正則把數(shù)據(jù)匹配出來 //3-4 緩存數(shù)據(jù)到數(shù)據(jù)庫(轉換編碼,把GBK轉換為Unicode) $items = array_combine($res[1],$res[2]); foreach ($items as $key => $val) { $ret[$key] = iconv('GB2312', 'UTF-8', $val); } } return $ret; } }
    查看全部
  • libs/ImRedis.php <?php /** * 獲取Redis數(shù)據(jù)庫句柄 */ namespace libs; class ImRedis{ private static $redis; public static function getRedis(){ //如果$redis不是\Redis的對象的話 if (!(self::$redis instanceof \Redis)) { self::$redis = new \Redis();//重新初始化一下 self::$redis->connect('127.0.0.1',6379);//連接redis } return self::$redis; } } 測試redis模塊能不能用 cmd cd D:\redis\redis-2.6 set name imooc get name QueryPhone.php <?php namespace app; use libs\ImHttpRequest; use libs\ImRedis; class QueryPhone{ public static function query($phone){ if (self::verifyPhone($phone)){ //測試是否能打印出來$name $name = ImRedis::getRedis()->get('name'); var_dump($name); die; } } }
    查看全部
  • 3-4 緩存數(shù)據(jù)到數(shù)據(jù)庫 cmd cd D:\redis\redis-2.6(需切換到redis根目錄) redis-server.exe redis.conf #啟動redis數(shù)據(jù)庫服務 redis-cli.exe -h 127.0.0.1 -p 6379 #設置redis的IP地址和端口 set key1 helloredis #創(chuàng)建一條數(shù)據(jù)(key-value) get key1 #獲取數(shù)據(jù)(key) redis-cli #查看redis是否啟動 keys * #查看所有的數(shù)據(jù) flushdb #清空數(shù)據(jù)庫
    查看全部
  • 3-3 格式化數(shù)據(jù) QueryPhone.php namespace app; use libs\ImHttpRequest; class QueryPhone{ //3-2 API請求數(shù)據(jù) const TAOBAO_API = 'https://tcc.taobao.com/cc/json/mobile_tel_segment.htm'; public static function query($phone){ //var_dump($phone); //3-1 校驗手機號碼合法性 //var_dump(self::verifyPhone($phone)); //3-2 API請求數(shù)據(jù) if (self::verifyPhone($phone)){ $response = ImHttpRequest::request(self::TAOBAO_API,['tel'=>$phone]); //var_dump($response); //3-3 格式化數(shù)據(jù) //self::formatData($response);//調(diào)用formatData方法 $data = self::formatData($response); if ($data) { var_dump($data); } } } /** * 3-3 格式化API請求回來的數(shù)據(jù) * @param null $data * @return array|bool */ public static function formatData($data = null){ $ret = false; if ($data) { preg_match_all("/(\w+):'([^']+)/",$data,$res);//用正則把數(shù)據(jù)匹配出來 //var_dump($res);//數(shù)據(jù)中的key和value分割為兩組數(shù)組元素 $ret = array_combine($res[1],$res[2]);//合并key和value } return $ret; } }
    查看全部
  • 3-2 API請求數(shù)據(jù) QueryPhone.php namespace app; use libs\ImHttpRequest; class QueryPhone{ //3-2 API請求數(shù)據(jù) const TAOBAO_API = 'https://tcc.taobao.com/cc/json/mobile_tel_segment.htm'; public static function query($phone){ //3-2 API請求數(shù)據(jù) if (self::verifyPhone($phone)){ $response = ImHttpRequest::request(self::TAOBAO_API,['tel'=>$phone]); var_dump($response); } } }
    查看全部
  • 3-2 API請求數(shù)據(jù) <?php /** * HTTP請求模塊 */ namespace libs; class ImHttpRequest{ public static function request($url, $params, $method = 'GET'){ $response = null;//定義默認的返回值為null if ($url){ $method = strtoupper($method);//全轉換為大寫 if ($method == 'POST'){ }elseif ($method == 'PUT'){ }elseif ($method == 'DELETE'){ }else{ if (is_array($params) and count($params)){ if (strrpos($url, '?')) { $url = $url . '&' . http_build_query($params); }else{ $url = $url . '?' . http_build_query($params); } //var_dump($url); $response = file_get_contents($url); } } } //var_dump($response);//需要開啟php/php.ini中的openssl擴展 return $response; } }
    查看全部
  • QueryPhone.php public static function query($phone){ //3-1 校驗手機號碼合法性 var_dump(self::verifyPhone($phone)); } /** * 3-1 校驗手機號碼合法性 * @param null $phone * @return bool */ public static function verifyPhone($phone = null){ $ret = false;//默認為false if ($phone){ //正則匹配 if (preg_match('/^1[34578]{1}\d{9}/',$phone)){ $ret = true; } } return $ret; }
    查看全部
  • 框架結構
    查看全部
  • 2-3 信息查詢流程解析 號碼信息查詢流程
    查看全部
  • 全局都使用了自動加載的機制 QueryPhone.php namespace app; class QueryPhone{ public static function test(){ //var_dump('imooc'); QueryIP::query(); } api.php app\QueryPhone::test(); app\QueryIP::query(); QueryIP.php namespace app; class QueryIP{ public static function query(){ var_dump('ip query'); } } 瀏覽器:http://127.0.0.1:9100/api.php
    查看全部
  • 自動加載方式 QueryPhone.php namespace app; class QueryPhone{ public static function query($phone){ var_dump($phone); } } api.php require_once "autoload.php"; app\QueryPhone::query('15987654321'); class autoload{ public static function load($className){ //$fileName = str_replace('\\','/',$className);//轉義目錄的/ $fileName = sprintf('%s.php',str_replace('\\','/',$className));//占位.php并轉義/ //var_dump($fileName); if (is_file($fileName)) require_once $fileName;//如果結果打印出來了就證明加載成功了 } } //注冊到spl的autoload里 spl_autoload_register(['autoload','load']); 瀏覽器:http://127.0.0.1:9100/api.php
    查看全部

舉報

0/150
提交
取消
課程須知
學習本門課程之前,建議先了解一下知識,會更有助于理解和掌握本門課程 1、PHP基礎知識 2、了解面向對象 3、對redis有一個基本了解
老師告訴你能學到什么?
1、合理規(guī)劃框架結構 2、類庫自動加載 3、項目流程分析 4、Redis基礎應用

微信掃碼,參與3人拼團

微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號

友情提示:

您好,此課程屬于遷移課程,您已購買該課程,無需重復購買,感謝您對慕課網(wǎng)的支持!