Home  >  Article  >  php教程  >  使用淘宝IP库获取用户ip地理位置

使用淘宝IP库获取用户ip地理位置

WBOY
WBOYOriginal
2016-06-06 20:27:011336browse

以前用过GOOGLE地图和百度地图获取过用户地理位置,现在又多了一个方法,那就是使用淘宝IP库获取用户ip地理位置,一起来看代码吧。

淘宝公布了他们的IP库,,还有REST API接口,不过每个用户的访问频率需小于10qps,访问方    式:?ip=[ip地址字串],返回内容以json格式的。具有IP查询,IP统计等功能。各大运营商拥有的IP数等信息。接下来介绍一下获取ip的实例:

复制代码 代码如下:


 /**
  * 通过淘宝IP接口获取IP地理位置
  * @param string $ip
  * @return: string
  **/
  function getCity($ip) 
  { 
  $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; 
  $ipinfo=json_decode(file_get_contents($url)); 
  if($ipinfo->code=='1'){ 
  return false; 
  } 
  $city = $ipinfo->data->region.$ipinfo->data->city; 
  return $city; 
  }   
  header("Content-Type:text/html;charset=utf-8"); 
  var_dump(getCity("112.234.69.189")); 
?> 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn