php使用GeoIP庫實(shí)例
需要注意的是maxmind是geoip數(shù)據(jù)庫的提供方,
同時(shí)也提供了很多語言的sample和api說明文檔。
比如php,和php的geoip庫是有很大區(qū)別的,包含使用方式,接口函數(shù)。
php官方的geoip需要配置php環(huán)境,加載geoip.dll庫,并在php.ini中指定GEOIP庫地址。
maxmind提供一系列 .inc 和 .php 對環(huán)境無依賴,只要支持php,直接require后即可使用。
一、GEOIP數(shù)據(jù)庫
http://dev.maxmind.com/geoip/geolite
細(xì)分到國家:GeoLite Country
細(xì)分到城市:GeoLite City
二、php官方geoip.dll庫
下載dll http://windows.php.net/downloads/pecl/releases/geoip/1.0.8/
修改php.ini,啟用geoip庫:
追加geoip段,指定數(shù)據(jù)庫位置:
geoip.custom_directory = "D:\php5.3\geo\"
測試代碼
注意geoip.dll中使用的 GeoIPCity.dat 即 GeoLiteCity.dat,用的時(shí)候注意看提示
echo geoip_country_name_by_name( "8.8.8.8" ) . "\n";
print_r( geoip_record_by_name( "8.8.8.8" ) );
echo geoip_country_name_by_name( "61.139.2.69" ). "\n";
print_r( geoip_record_by_name( "61.139.2.69" ) );
三、MaxMind官方php文件函數(shù)庫
文檔和示例:http://dev.maxmind.com/geoip/downloadable
修改maxmind示例中 sample.php 和 sample_city.php 中 GeoIP.dat/GeoLiteCity.dat 路徑為你自己的路徑
同一目錄用 “./GeoIP.dat” 或 “./GeoLiteCity.dat” 即可。
詳細(xì)到國家
include("geoip.inc");
$gi = geoip_open( "./GeoIP.dat", GEOIP_STANDARD );
echo geoip_country_code_by_addr($gi, "8.8.8.8") . "\t" . geoip_country_name_by_addr($gi, "8.8.8.8") . "\n";
echo geoip_country_code_by_addr($gi, "61.139.2.69") . "\t" . geoip_country_name_by_addr($gi, "61.139.2.69") . "\n";
geoip_close($gi);
詳細(xì)到國家城市
include("geoipcity.inc");
include("geoipregionvars.php");
$gi = geoip_open("./GeoLiteCity.dat",GEOIP_STANDARD);
$record = geoip_record_by_addr($gi,"8.8.8.8");
print $record->country_code . " " . $record->country_code3 . " " . $record->country_name . "\n";
print $record->region . " " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . "\n";
print $record->city . "\n";
print $record->postal_code . "\n";
print $record->latitude . "\n";
print $record->longitude . "\n";
print $record->metro_code . "\n";
print $record->area_code . "\n";
print $record->continent_code . "\n";
print "\n-----\n";
$record = geoip_record_by_addr($gi,"61.139.2.69");
print $record->country_code . " " . $record->country_code3 . " " . $record->country_name . "\n";
print $record->region . " " . $GEOIP_REGION_NAME[$record->country_code][$record->region] . "\n";
print $record->city . "\n";
print $record->postal_code . "\n";
print $record->latitude . "\n";
print $record->longitude . "\n";
print $record->metro_code . "\n";
print $record->area_code . "\n";
print $record->continent_code . "\n";
geoip_close($gi);
看自己開發(fā)環(huán)境和具體情況決定用哪種
- php GeoIP的使用教程
- PHP安裝GeoIP擴(kuò)展根據(jù)IP獲取地理位置及計(jì)算距離的方法
- PHP計(jì)算百度地圖兩個(gè)GPS坐標(biāo)之間距離的方法
- php實(shí)現(xiàn)計(jì)算百度地圖坐標(biāo)之間距離的方法
- php+js實(shí)現(xiàn)百度地圖多點(diǎn)標(biāo)注的方法
- php使用google地圖應(yīng)用實(shí)例
- 定位地理位置PHP判斷員工打卡簽到經(jīng)緯度是否在打卡之內(nèi)
- php讀取qqwry.dat ip地址定位文件的類實(shí)例代碼
- PHP進(jìn)階學(xué)習(xí)之Geo的地圖定位算法詳解
相關(guān)文章
Drupal7中常用的數(shù)據(jù)庫操作實(shí)例
Drupal 7 提供的新功能其中一個(gè)就是可以使用 Query Builder and Query Objects 查詢生成器來構(gòu)造查詢對象的能力,無需在代碼中寫原始的SQL語句,一是提高了代碼可閱讀性,二是兼容所有數(shù)據(jù)庫2014-03-03
PHP curl實(shí)現(xiàn)抓取302跳轉(zhuǎn)后頁面的示例
這篇文章主要介紹了PHP curl實(shí)現(xiàn)抓取302跳轉(zhuǎn)后頁面的示例,主要是對CURLOPT_CUSTOMREQUEST參數(shù)的運(yùn)用,需要的朋友可以參考下2014-07-07
Smarty最簡單實(shí)現(xiàn)列表奇偶變色的方法
這篇文章主要介紹了Smarty最簡單實(shí)現(xiàn)列表奇偶變色的方法,實(shí)例分析了Smarty屬性與方法的相關(guān)使用技巧,需要的朋友可以參考下2015-07-07
PHP函數(shù)addslashes和mysql_real_escape_string的區(qū)別
這篇文章主要介紹了PHP函數(shù)addslashes和mysql_real_escape_string的區(qū)別,以及一個(gè)SQL注入漏洞介紹,需要的朋友可以參考下2014-04-04
使用JavaScript創(chuàng)建新樣式表和新樣式規(guī)則
這篇文章主要介紹了使用JavaScript創(chuàng)建新樣式表和新樣式規(guī)則的相關(guān)資料,需要的朋友可以參考下2016-06-06
關(guān)于 Laravel Redis 多個(gè)進(jìn)程同時(shí)取隊(duì)列問題詳解
這篇文章主要給大家介紹了關(guān)于 Laravel Redis 多個(gè)進(jìn)程同時(shí)取隊(duì)列問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12

