Home  >  Article  >  Backend Development  >  使用淘宝IP库获取用户ip地理位置_php实例

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

WBOY
WBOYOriginal
2016-05-17 08:54:071143browse

淘宝公布了他们的IP库http://ip.taobao.com/,还有REST API接口,不过每个用户的访问频率需小于10qps,访问方    式:http://ip.taobao.com/service/getIpInfo.php?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