手机号码归属地查询(php+redis)

26 篇文章 0 订阅
4 篇文章 0 订阅

这次做手机号码的归属地查询主要学到了redis和正则

可以直接下载代码 https://github.com/SmallLeaves/mobile-phone-home.git

也可以复制下面的代码

index.html

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>手机归属地查询</title>
	<link href="https://cdn.bootcss.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet">
	<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
	<script src=""></script>
	<style type="text/css">
		body {
			max-width: 720px;
			margin:  0 auto;
		}
		.top-20 {
			margin-top: 20px;
		}
		.table-width {
			margin-left: 20px;
			width: 95%;
		}
		#phoneInfo {
			display: none;
		}
	</style>
</head>
<body>
<div class="row top-20">
	<div class="col-xs-9">
			<input type="text" id="phoneText" class="form-control" name="" value="" >
	</div>
	<div class="col-xs-3">
		<input type="button" style="margin-left: 10px;" class="btn  btn-block btn-primary"  id="subPhone" value="查询" οnkeypress="if(event.keyCode==13) {btnValidate.click();return false;}" >
	</div>
</div> 
	<div class="row top-20" id="phoneInfo">
	<table class="table table-width table-responsive" >
		<tr>
			<td>手机号码</td>
			<td id="phoneNumber"></td>
		</tr>
		<tr>
			<td>归属地</td>
			<td id="phoneProvince"></td>
		</tr>
		<tr>
			<td>运营商</td>
			<td id="phoneCatName"></td>
		</tr>
		<tr>
			<td>其他</td>
			<td id="phoneMsg"></td>
		</tr>
	</table>

	</div>

</body>
</html>
<script>
//输入框按回车触发查询按钮
$(document).keyup(function(event){
 if(event.keyCode ==13){
  $("#subPhone").trigger("click");
 }
});
	$(document).ready(function(){
		$('#subPhone').click(function(){
			// 获取手机号码
			var phone = document.getElementById('phoneText').value;
			var url = 'api.php';
			var postData={phone:phone};
			// 将数据传到api.php  获取手机号的信息
			$.post(url,postData,function(response){
				if(response.length==51){
					alert('请输入有效的手机号码');
				}else{
				// 将返回的手机号信息进行各种格式化,转成自己想要的样子
				var reg = RegExp('\\s+','g');
				var regYinhao = RegExp('"','g');
				response = response.replace(reg,'');
				response = response.split('"\[');
				var arr = [];
			 	response.forEach(function(value, key) {
			 		value = value.replace('array(8){[','');
			 		value = value.replace('}','');
			 		value = value.replace(regYinhao,'');
					key = value.match(/(\w+)\]/)[1];
			 		value = value.match(/\)(\S+)/)[1];
				    arr[key] =value;
				});
				// 将数据传到表格里面
				$('#phoneNumber').text(arr['telString']);
				$('#phoneProvince').text(arr['province']);
				$('#phoneCatName').text(arr['catName']);
				$('#phoneMsg').text(arr['msg']);
				$('#phoneInfo').show();
				}
			});
		});
	});
</script>

api.php

<?php 
require_once "autoload.php";
$phone = isset($_POST['phone'])?$_POST['phone']:null;
$phoneInfo = app\Queryphone::query($phone);
var_dump($phoneInfo);//一定要输出  之前不小心把这个删掉了   index.html里面的console.log(response);不显示数据
 ?>

app文件夹下的Queryphone.php

<?php 
namespace app;
use libs\ImHttpRequest;
use libs\ImRedis;
class Queryphone{
	const TAOBAO_API = 'http://tcc.taobao.com/cc/json/mobile_tel_segment.htm';
	const CACHE_KEY = 'PHONE:INFO';
	/*查询手机号码的归属地*/
	/*
	验证手机号是否合法
	根据TAOBAO_API查看手机归属地
	将手机号信息保存到redis
	*/
	public static function query($phone){
		$ret = [];
		if(self::verifyPhone($phone)){
			// $redisKey = substr($phone, 0,7);
			$phoneInfo = ImRedis::getRedis()->hGet(self::CACHE_KEY,$phone);
			if($phoneInfo){
				$ret = json_decode($phoneInfo,true);
				$ret['msg']='由"林中落日"提供数据';
			}else{
				$response =  ImHttpRequest::request(self::TAOBAO_API,['tel'=>$phone],$method='GET');
				 $response=iconv("GBK", "UTF-8//IGNORE", $response);//直接输出$response乱码
				 $data = self::formatData($response);
				 if($data){
				 	// var_dump($data);
				 	$json = json_encode($data);
				 	ImRedis::getRedis()->hSet(self::CACHE_KEY,$phone,$json);
				 	$data['msg']='由"追风去"提供数据';
				 	$ret = $data;
					}
			}
		}else{
			$ret['msg']= '请输入有效的手机号码';
		}
		return $ret;
	}
	/*检验手机号码的合法性*/
	public static function verifyPhone($phone = null){
		$ret =false;
		if($phone){
			if(preg_match('/^1[34578]{1}\d{9}/', $phone)){
				$ret = true;
			}
		}
		return $ret;
	}
	/*将API请求回来的数据转化成数组的形式*/
	public static function formatData($data=null){
		$ret = false;
		if($data){
			preg_match_all("/(\w+):'([^']+)/", $data, $ret);
			$ret = array_combine($ret[1], $ret[2]);
		}
		return $ret;
	}
}

 ?>

libs文件夹下的ImRedis.php

<?php 
 namespace libs;
 class ImRedis{
 	private static $redis;
 	public static function getRedis(){
 		if(!(self::$redis instanceof \Redis)){
 			self::$redis=new \Redis;
 			self::$redis->connect('127.0.0.1', 6379);
 		}
 		return self::$redis;
 	}
 }
 ?>

libs文件夹下的ImHttpRequest.php

<?php 
namespace libs;
class ImHttpRequest{
	public static function request($url,$params,$method='GET'){
		$response = null;
		if($url){
			$method = strtoupper($method);
			if($method == 'POST'){

			}elseif($method == 'PUT'){

			}elseif($method == 'DELETE'){

			}else{
				// var_dump(strrpos($url, '?'));
				if(is_array($params) and count($params)){
					if(strrpos($url, '?')){
						$url = $url.'&'.http_build_query($params);
					}else{
						$url = $url.'?'.http_build_query($params);
					}
					$response = file_get_contents($url);
				}
			}
		}
		return $response;
	}
}

autoload.php

<?php 
class autoload{
	public static function load($className){
		$fileName = sprintf('%s.php',str_replace('\\', '/', $className));
		if(is_file($fileName)) require_once $fileName;
	}
}
spl_autoload_register(['autoload','load']);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值