PHP 编程 · 2014/02/25 0

PHP 安装 geoip 扩展

安装对应的 dev 包
Ubuntu 或 Debian 系列下:
sudo apt-get install geoip-bin geoip-database libgeoip-dev

CentOS 下:
sudo yum install re2c geoip geoip-data geoip-devel

下载Lite城市库 (此步骤可以省略, 但是测试代码未必可用)
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gunzip GeoLiteCity.dat.gz
sudo mkdir -v /usr/share/GeoIP
sudo mv -v GeoLiteCity.dat /usr/share/GeoIP/GeoIPCity.dat

安装PHP geoip 扩展
sudo apt-get install php5-geoip
或者
sudo pecl install geoip
或者自己手动编译安装

测试:
php -r "print_r(geoip_record_by_name('weibo.com'));"
结果如下:
Array
(
[continent_code] => AS
[country_code] => CN
[country_code3] => CHN
[country_name] => China
[region] => 22
[city] => Beijing
[postal_code] =>
[latitude] => 39.928901672363
[longitude] => 116.38829803467
[dma_code] => 0
[area_code] => 0
)