<?PHP
$addr_xyz=$_SERVER["REMOTE_ADDR"];
//获取用户IP
function check_address($ip){

if(empty($ip)){

	return  '缺少用户ip';

}


$url = 'https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query='.$ip.'&co=&resource_id=6006&t=&ie=utf8&oe=gbk&cb=op_aladdin_callback&format=json&tn=baidu&cb=&_=';
//调用了百度接口
$str = file_get_contents($url);
//获取返回内容

$encode = mb_detect_encoding($str, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5')); 

$str = mb_convert_encoding($str, 'UTF-8', $encode);
//转化编码
$str=json_decode($str);
//转换为json类型
$str=$str->data[0]->location;
//取出数据
return $str;
}
echo check_address($addr_xyz);
?>

接口的话直接用的百度搜索返回抓的,可能比较慢