杨小杰博客提供免费教程下载和网站搭建技术教程,主要分享和发布网站源码,致力创造一个高质量网络资源教程的分享平台

正则获取手机号归属地api源码

Young小杰2018-7-14 14:59网站搭建(1)225查询中……小标签: 杨小杰 api 原创

一网友需要,所以把源码放出来,又是蹩脚正则实现的。


<?php
/**
 * 手机号归属地查询
 * @author Youngxj <blog@youngxj.cn>
 * @time   2018年7月14日
 * @code   200->正常;201手机号不正确;202手机号无数据
 */
header("Access-Control-Allow-Origin:*");
header('Content-type: application/json');
error_reporting(0);

if(isset($_GET['tel'])&&is_numeric($_GET['tel'])){
	$tel = $_GET['tel'];
}else{
	echo json_encode(array('code'=>'201','msg'=>'不是正确的手机号'));
	exit();
}
/*获取接口数据*/
$string = httpGet('https://shouji.supfree.net/fish.asp?cat='.$tel);
/*编码转换*/
$string = mb_convert_encoding($string,'utf-8', 'gbk');
/*正则查找*/
preg_match_all('/<p>(.*)<\/p>/',$string,$str);

$local = strip_tags($str[1][0]);
$duan = strip_tags($str[1][1]);
$type = strip_tags($str[1][2]);
$yys = strip_tags($str[1][3]);
$bz = strip_tags($str[1][5]);

if($local!=''){
	echo json_encode(array('code'=>'200','tel'=>$tel,'local'=>$local,'duan'=>$duan,'type'=>$type,'yys'=>$yys,'bz'=>$bz));
}else{
	echo json_encode(array('code'=>'202','msg'=>'该手机号无数据'));
	exit();
}

function httpGet($a, $b = '', $c = '', $d = ''){
	/*curl模拟get请求*/
	$e = curl_init();
	$f = mt_rand(11, 191) . "." . mt_rand(0, 240) . "." . mt_rand(1, 240) . "." . mt_rand(1, 240);
	$i[] = "CLIENT-IP:" . $f;
	$i[] = "X-FORWARDED-FOR:" . $f;
	$i[] = "User-agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11";
	$i[] = "X-Requested-With: XMLHttpRequest";
	if (!empty($d)) {
		$i[] = "Cookie: " . $d;
	}
	curl_setopt($e, CURLOPT_HTTPHEADER, $i);
	curl_setopt($e, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($e, CURLOPT_TIMEOUT, 180);
	curl_setopt($e, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($e, CURLOPT_SSL_VERIFYHOST, false);
	if (!empty($c)) {
		curl_setopt($e, CURLOPT_REFERER, $c);
	}
	if (!empty($b)) {
		curl_setopt($e, CURLOPT_POST, 1);
		curl_setopt($e, CURLOPT_POSTFIELDS, $b);
	}
	curl_setopt($e, CURLOPT_URL, $a);
	curl_setopt($e, CURLOPT_ENCODING, "gzip");
	$j = curl_exec($e);
	curl_close($e);
	return $j;
}
手机号归属地.png

评论:

小石博客 Google Chrome 44.0.2403.119 Linux广西桂林市 电信 2018-07-14 15:10
支持我的小杰

发表评论: