GeoIP PHP扩展安装与配置

WBOY
發布: 2016-06-23 13:43:58
原創
1265 人瀏覽過

  作者:zhanhailiang 日期:2014-12-08
登入後複製

1. 安装依赖GeoIP-devel,GeoIP

GeoIP-devel.x86_64 : Development headers and libraries for GeoIPGeoIP.x86_64 : Library for country/city/organization to IP address or hostname mapping
登入後複製
yum install GeoIP.x86_64 GeoIP-devel.x86_64
登入後複製

2. 配置IP数据库GeoIP.dat,GeoLiteCity.dat

wget -N http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gzgunzip GeoIP.dat.gzmv GeoIP.dat /usr/local/share/GeoIP/
登入後複製
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gzgunzip GeoLiteCity.dat.gzmkdir -v /usr/share/GeoIPmv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat
登入後複製
说明 :GeoIPRegion.dat是付费服务;
登入後複製

参考:

  • http://dev.maxmind.com/geoip/legacy/install/country/

  • http://forum.directadmin.com/showthread.php?t=43309

  • 3. 安装扩展

    git clone git@github.com:billfeller/geoip.git; phpize./configure --with-php-config=/usr/local/php/bin/php-config --with-geoipmake && make install
    登入後複製

    4. 测试

    [root@~/wade/pecllearning/geoip]# /usr/local/php/bin/php -r 'print geoip_country_code3_by_name("220.181.112.244").PHP_EOL;'CHN[root@~/wade/pecllearning/geoip]# /usr/local/php/bin/php -r 'print_r(geoip_record_by_name("php.net"));'Array(    [continent_code] => NA    [country_code] => US    [country_code3] => USA    [country_name] => United States    [region] => CA    [city] => Fremont    [postal_code] => 94539    [latitude] => 37.51549911499    [longitude] => -121.8962020874    [dma_code] => 807    [area_code] => 510)
    登入後複製

    注意,由于本地无GeoIPRegion.dat,调用 geoip_region_by_name 时会报Warning,如下

    [root@~/wade/pecllearning/geoip]# /usr/local/php/bin/php -r 'print geoip_region_by_name("220.181.112.244");'PHP Warning:  geoip_region_by_name(): Required database not available at /usr/share/GeoIP/GeoIPRegion.dat. in Command line code on line 1 Warning: geoip_region_by_name(): Required database not available at /usr/share/GeoIP/GeoIPRegion.dat. in Command line code on line 1
    登入後複製

    來源:php.cn
    本網站聲明
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
    熱門教學
    更多>
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板