php获取手机号码归属地

tel.php代码

<?php
header("Content-type:text/html;charset=utf-8");
function getMobileInfo($mobile){
    
    
	$sms = array('province'=>'', 'supplier'=>'');    //初始化变量
	//根据淘宝的数据库调用返回值
	$url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query={
      
      $mobile}&resource_id=6004&ie=utf8&oe=utf8&format=json";
	$content = file_get_contents($url);
	// echo($content);
	$result=json_decode($content);
	// print_r($result);
	if(empty($result->data[0])){
    
    
		return("对不起,没有查询到 {
      
      $mobile} 信息");
	}
	else{
    
    
		$arr=$result->data[0];
		return $arr->prov."-".$arr->city;
	}
	
}
$tel=$_REQUEST["tel"];
echo(getMobileInfo($tel));
?>

使用方法
ps:返回值为text

$.ajax({
    
    
	 "type":"post",
	  "url":"http://localhost/res/php/tel.php",
	  "data":{
    
    "tel":tel},
	  "error":function(){
    
    
		  alert("服务器请求出错!");
	  },
	  "success":function(data){
    
    
		  $("#msg").html(data);
	  }
 });
   		  

猜你喜欢

转载自blog.csdn.net/hifhf/article/details/107878793