dpodium/yii2-geoip

Yii2 组件,允许轻松使用 MaxMind 免费数据库。

安装次数: 24,888

依赖者: 2

建议者: 0

安全性: 0

星星: 8

关注者: 6

分支: 3

开放问题: 2

类型:yii2-extension

2.1.19 2024-06-27 04:03 UTC

README

Yii2 组件,允许轻松使用 MaxMind 免费数据库。

基于 phiphi1992/Yii2-GeoIP 包,由 Phi Hoang Xuan 开发。

安装

安装此扩展的首选方法是通过 composer

运行以下命令:

php composer.phar require dpodium/yii2-geoip "~2.0.0"

或者将以下内容添加到你的 composer.json 文件的 require 部分:

"dpodium/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' => 'dpodium\yii2\geoip\components\CGeoIP',
            'support_ipv6' => false, //Default value
        ],
        ...
    ],
    ...
];

如果需要查询完整的城市数据,还需要要求 yii2-geoip-city-db,请参阅 dpodium/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