Use Taobao IP library to get user ip location

  • 2020-10-23 20:55:27
  • OfStack

Taobao announced their IP library http: / / ip taobao. com /, and REST API interface, but each user's access frequency should be less than 10 qps, access mode: http: / / ip taobao. com/service/getIpInfo php? ip=[ip address string], which returns the contents in json format. IP query, IP statistics and other functions. Information such as the number of IP held by each operator. The following is an example of acquiring ip:

<?php
 /** 
  *  Through taobao IP Interface to get IP The geographical position  
  * @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 articles: