欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php利用新浪接口查询ip获取地理位置示例

程序员文章站 2022-06-20 23:47:51
复制代码 代码如下:

复制代码 代码如下:

<?php
function getiploc_sina($queryip){   
$url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='.$queryip;   
$ch = curl_init($url);    
curl_setopt($ch,curlopt_encoding ,'utf8');    
curl_setopt($ch, curlopt_timeout, 5);  
curl_setopt($ch, curlopt_returntransfer, true) ; // 获取数据返回 
$location = curl_exec($ch);   
$location = json_decode($location);   
curl_close($ch);        
$loc = "";  
if($location===false) return "";    
if (empty($location->desc)) {   
$loc = $location->province.$location->city.$location->district.$location->isp; 
}else{         $loc = $location->desc;   
}   
return $loc;
}
echo getiploc_sina("183.37.209.57");
?>