izisoft / yii2-geoip
Yii2 组件,允许轻松使用 MaxMind 免费数据库。
v3.0
2018-10-27 21:16 UTC
Requires
- geoip2/geoip2: ~2.0
- yiisoft/yii2: 2.0.*
README
Yii2 组件,允许轻松使用 MaxMind 免费数据库。
基于 phiphi1992/Yii2-GeoIP 包,由 Phi Hoang Xuan 开发。
安装
安装此扩展的首选方式是通过 composer。
运行以下命令之一:
php composer.phar require izisoft/yii2-geoip "~2.0.0"
或者将以下内容添加到您的 composer.json
文件的 require 部分:
"izisoft/yii2-geoip": "~2.0.0"
版本 ~2.0.0 的差异
版本 ~2.0.0 使用新的 GeoLite2 数据库版本,而不是现在已弃用的 Legacy GeoLite。
数据库可以在以下位置找到: https://dev.maxmind.com/geoip/geoip2/geolite2/
组件设置
扩展安装后,只需按如下方式修改您的应用程序配置:
return [ 'components' => [ ... 'geoip' => [ 'class' => 'izi\geoip\components\CGeoIP', ], ... ], ... ];
如果需要查询完整的城市数据,则必须同时引入 yii2-geoip-city-db,请参阅 izisoft/yii2-geoip-city-db。
有关数据可用性的更多信息,请参阅以下内容。
用法
所有方法都接受一个 IP 地址作为参数。如果没有提供参数,则使用 Yii::$app->getRequest()->getUserIP()。
//Along with free DB
$location = Yii::$app->geoip->lookupLocation();
$countryCode = Yii::$app->geoip->lookupCountryCode();
$countryName = Yii::$app->geoip->lookupCountryName();
位置属性
$location->countryCode //Available in both Country and City DB
$location->countryName //Available in both Country and City DB
$location->continentCode //Available in both Country and City DB
$location->continentName //Available in both Country and City DB
$location->city //Available in only City DB
$location->postalCode //Available in only City DB
$location->latitude //Available in only City DB
$location->longitude //Available in only City DB
$location->timeZone //Available in only City DB