Home  >  Article  >  Backend Development  >  php获取指定(访客)IP所有信息(地址、邮政编码、国家、经纬度等)的方法_php技巧

php获取指定(访客)IP所有信息(地址、邮政编码、国家、经纬度等)的方法_php技巧

WBOY
WBOYOriginal
2016-05-16 20:11:371080browse

本文实例讲述了php获取指定(访客)IP所有信息(地址、邮政编码、国家、经纬度等)的方法。分享给大家供大家参考。具体如下:

调用方法非常简单。这个也需要数据库来支持。数据库中中文和拼音共存才可以。

废话不多说,看代码:

<&#63;php  
function getIpInfo($ip,$timeout=15) {  
  if(!function_exists('curl_init') or !function_exists('simplexml_load_string')) return false;  
  $ch = curl_init("http://ipinfodb.com/ip_query2.php&#63;ip={$ip}&timezone=true");  
  $options = array(  
      CURLOPT_RETURNTRANSFER => true,  
    );  
  curl_setopt_array($ch,$options);  
  $res = curl_exec($ch);  
  curl_close($ch);  
  
  if($xml = simplexml_load_string($res)) {  
    $return = array();  
    foreach ($xml->Location->children() as $key=>$item) {  
      $return[$key] = strtolower($item);  
    }  
    return $return;  
  } else {  
    return false;  
  }  
}  
$current_Ip_Info = getIpInfo('119.7.8.255');  
var_dump($current_Ip_Info); 

希望本文所述对大家的php程序设计有所帮助。

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