通过GeoIP获取ip所属地 (国家,城市,时区,邮编,经纬度等)

来源:互联网 发布:simulink算法模型 编辑:程序博客网 时间:2024/04/28 00:38


来源: http://www.phpandstuff.com/articles/geoip-country-lookup-with-php

 

GeoIP + PHP

Php代码  收藏代码
  1. <?php  
  2.   
  3.     //计时开始  
  4.     function utime() {  
  5.         $time = explode" ", microtime() );  
  6.         $usec = (double)$time[0];  
  7.         $sec = (double)$time[1];  
  8.         return $usec + $sec;  
  9.     }  
  10.     $startTimes = utime();  
  11.    
  12.     // include the php script  
  13.     // wget -c http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz  
  14.     // gunzip GeoIP.dat.gz  
  15.     include("geoip.inc");  
  16.    
  17.     // open the geoip database  
  18.     $gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);  
  19.    
  20.     // 获取国家代码  
  21.     $country_code = geoip_country_code_by_addr($gi$_SERVER['REMOTE_ADDR']);  
  22.     echo "Your country code is: <strong>$country_code</strong> <br />";  
  23.    
  24.     // 获取国家名称  
  25.     $country_name = geoip_country_name_by_addr($gi$_SERVER['REMOTE_ADDR']);  
  26.     echo "Your country name is: <strong>$country_name</strong> <br />";  
  27.    
  28.     // close the database  
  29.     geoip_close($gi);  
  30.    
  31.     //运行结束时间  
  32.     $endTimes = utime();  
  33.     $runTimes = sprintf( '%0.4f', ( $endTimes - $startTimes ) );  
  34.     echo "Processed in " . $runTimes . "second.";  
  35. ?>  

 

注:在本地测试的话因 为$_SERVER['REMOTE_ADDR']和$_SERVER['REMOTE_ADDR']可能是127.0.0.1,所 以输出的内容为空。可以自己带入IP测试

 

或者 使用某网站的API

 

API 1.

 

1. 返回文字

 

Html代码  收藏代码
  1. http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true  
 

2. 返回图片

 

Html代码  收藏代码
  1. <IMG SRC="http://api.hostip.info/flag.php?ip=12.215.42.19" ALT="IP Address Lookup">  
 

 

API 2. (需要申请api key ,免费的,类似google)

 

城市:

 

Html代码  收藏代码
  1. http://api.ipinfodb.com/v3/ip-city/?key=<your_api_key>&ip=74.125.45.100  

 

国家(更快) :

 

Html代码  收藏代码
  1. http://api.ipinfodb.com/v3/ip-country/?key=<your_api_key>&ip=74.125.45.100  
 

 

Parameter Required Default ValuekeyYes<empty>API key provided with your free account.ipNoClient IPIP addressformatNorawraw, xml, jsoncallbackNo<empty>Required when using json callback.Please use the appropriate API for your needs. You can help us keep the load low on our servers by making sure that :
  • If you only need the country name, avoid using the city precision API.
  • If you track your visitors, avoid querying our API for all your page views (you can store the geolocation in a cookie, see below for an example)

API Precision Timezone Domains lookupsip-cityCityYesYesip-countryCountryNoYesPlease use the appropriate API for your needs. You can help us keep the load low on our servers by making sure that :
  • If you only need the country name, avoid using the city precision API.
  • If you track your visitors, avoid querying our API for all your page views (you can store the geolocation in a cookie, see below for an example)

 

使用类:

 

Php代码  收藏代码
  1. <?php  
  2. final class ip2location_lite{  
  3.     protected $errors = array();  
  4.     protected $service = 'api.ipinfodb.com';  
  5.     protected $version = 'v3';  
  6.     protected $apiKey = '';  
  7.   
  8.     public function __construct(){}  
  9.   
  10.     public function __destruct(){}  
  11.   
  12.     public function setKey($key){  
  13.         if(!emptyempty($key)) $this->apiKey = $key;  
  14.     }  
  15.   
  16.     public function getError(){  
  17.         return implode("\n"$this->errors);  
  18.     }  
  19.   
  20.     public function getCountry($host){  
  21.         return $this->getResult($host'ip-country');  
  22.     }  
  23.   
  24.     public function getCity($host){  
  25.         return $this->getResult($host'ip-city');  
  26.     }  
  27.   
  28.     private function getResult($host$name){  
  29.         $ip = @gethostbyname($host);  
  30.   
  31.         if(preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/'$ip)){  
  32.             $xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml');  
  33.   
  34.             try{  
  35.                 $response = @new SimpleXMLElement($xml);  
  36.   
  37.                 foreach($response as $field=>$value){  
  38.                     $result[(string)$field] = (string)$value;  
  39.                 }  
  40.   
  41.                 return $result;  
  42.             }  
  43.             catch(Exception $e){  
  44.                 $this->errors[] = $e->getMessage();  
  45.                 return;  
  46.             }  
  47.         }  
  48.   
  49.         $this->errors[] = '"' . $host . '" is not a valid IP address or hostname.';  
  50.         return;  
  51.     }  
  52. }  
  53. ?>  
 
Php代码  收藏代码
  1. <?  
  2. include('ip2locationlite.class.php');  
  3.   
  4. //Load the class  
  5. $ipLite = new ip2location_lite;  
  6. $ipLite->setKey('<your_api_key>');  
  7.    
  8. //Get errors and locations  
  9. $locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);  
  10. $errors = $ipLite->getError();  
  11.    
  12. //Getting the result  
  13. echo "<p>\n";  
  14. echo "<strong>First result</strong><br />\n";  
  15. if (!emptyempty($locations) && is_array($locations)) {  
  16.   foreach ($locations as $field => $val) {  
  17.     echo $field . ' : ' . $val . "<br />\n";  
  18.   }  
  19. }  
  20. echo "</p>\n";  
  21.    
  22. //Show errors  
  23. echo "<p>\n";  
  24. echo "<strong>Dump of all errors</strong><br />\n";  
  25. if (!emptyempty($errors) && is_array($errors)) {  
  26.   foreach ($errors as $error) {  
  27.     echo var_dump($error) . "<br /><br />\n";  
  28.   }  
  29. else {  
  30.   echo "No errors" . "<br />\n";  
  31. }  
  32. echo "</p>\n";  
 

 

数据库版:

 

 

IP geolocation databases download

Updated Mar 5 2011

Database Uncompressed Size(MB) IP Precision Data Provided FormatDB11.60123.123.123ISO country code, country nameCSVBINDB317.11123.123.123ISO country code, country name, state, cityCSVBINDB521.40123.123.123ISO country code, country name, state, city, latitude, longitudeCSVBINDB922.76123.123.123ISO country code, country name, state, city, latitude, longitude, ZIP codesCSVBINDB1123.28123.123.123ISO country code, country name, state, city, latitude, longitude, ZIP codes, time zoneCSVBIN

 

 

key: c9dcc88453e33a9e63ebad8d65f91583e87abd8185dd95f09fbeef6c62264f7d

 

其他参考

http://pecl.php.net/package/geoip

http://www.geoiptool.com/

http://www.hostip.info/use.html

http://phpweby.com/software/ip2country

http://www.ipinfodb.com/index.php

 

 

转帖注明出处:http://justcoding.iteye.com/blog/986355

 

本站链接:

php 显示ip所属地 (qq版)

 

 http://justcoding.iteye.com/blog/986355

http://blog.csdn.net/wangjia55/article/details/7843874

  • getip.zip (582.4 KB)
  • 下载次数: 89
  • ip2locationlite.zip (733 Bytes)
  • 下载次数: 61
  • phpwebyip2country.zip (2 MB)
  • 下载次数: 84
0 0
原创粉丝点击