php應用GeoIP庫執行個體教程

來源:互聯網
上載者:User

php應用GeoIP庫執行個體教程

注意,maxmind是geoip資料庫的提供方,同時也提供了很多語言的sample和api說明文檔。比如php,和php的geoip庫是有很大區別的,包含使用方式,介面函數。php官方的geoip需要配置php環境,載入geoip.dll庫,並在php.ini中指定GEOIP庫地址。maxmind提供一系列 .inc 和 .php 對環境無依賴,只要支援php,直接require後即可使用。(php GeoIP庫入門執行個體)

一、GEOIP資料庫http://dev.maxmind.com/geoip/geolite細分到國家:GeoLite Country細分到城市:GeoLite City

二、php官方geoip.dll庫下載dll http://windows.php.net/downloads/pecl/releases/geoip/1.0.8/修改php.ini,啟用geoip庫:

  1. extension=php_geoip.dll
複製代碼

追加geoip段,指定資料庫位置:

  1. [geoip]
  2. geoip.custom_directory = "D:\php5.3\geo\"
複製代碼

測試代碼注意geoip.dll中使用的 GeoIPCity.dat 即 GeoLiteCity.dat,使用時注意看提示。

  1. echo geoip_country_name_by_name( "8.8.8.8" ) . "\n";
  2. print_r( geoip_record_by_name( "8.8.8.8" ) );
  3. echo geoip_country_name_by_name( "61.139.2.69" ). "\n";
  4. print_r( geoip_record_by_name( "61.139.2.69" ) );
複製代碼

三、MaxMind官方php檔案函數庫文檔和樣本:http://dev.maxmind.com/geoip/downloadable修改maxmind樣本中 sample.php 和 sample_city.php 中 GeoIP.dat/GeoLiteCity.dat 路徑為你自己的路徑同一目錄用 “./GeoIP.dat” 或 “./GeoLiteCity.dat” 即可。

1,詳細到國家

  1. include("geoip.inc");
  2. $gi = geoip_open( "./GeoIP.dat", GEOIP_STANDARD );
  3. echo geoip_country_code_by_addr($gi, "8.8.8.8") . "\t" . geoip_country_name_by_addr($gi, "8.8.8.8") . "\n";
  4. echo geoip_country_code_by_addr($gi, "61.139.2.69") . "\t" . geoip_country_name_by_addr($gi, "61.139.2.69") . "\n";
  5. geoip_close($gi);
複製代碼

2,詳細到國家城市

  1. include("geoipcity.inc");
  2. include("geoipregionvars.php");
  3. $gi = geoip_open("./GeoLiteCity.dat",GEOIP_STANDARD);
  4. $record = geoip_record_by_addr($gi,"8.8.8.8");
  5. print $record->country_code . " " . $record->country_code3 . " " . $record->country_name . "\n";
  6. print $record->region . " " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . "\n";
  7. print $record->city . "\n";
  8. print $record->postal_code . "\n";
  9. print $record->latitude . "\n";
  10. print $record->longitude . "\n";
  11. print $record->metro_code . "\n";
  12. print $record->area_code . "\n";
  13. print $record->continent_code . "\n";
  14. print "\n-----\n";
  15. $record = geoip_record_by_addr($gi,"61.139.2.69");
  16. print $record->country_code . " " . $record->country_code3 . " " . $record->country_name . "\n";
  17. print $record->region . " " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . "\n";
  18. print $record->city . "\n";
  19. print $record->postal_code . "\n";
  20. print $record->latitude . "\n";
  21. print $record->longitude . "\n";
  22. print $record->metro_code . "\n";
  23. print $record->area_code . "\n";
  24. print $record->continent_code . "\n";
  25. geoip_close($gi);
複製代碼

在使用以上代碼時,可以根據開發環境與具體情況決定用哪種。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.