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

WBOY
Release: 2016-05-17 08:54:07
Original
1230 people have browsed it

淘宝公布了他们的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")); 
?> 
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!