用户1007017 / yii2-geoip2
Yii2 GeoIP2 扩展。根据IP地址返回国家、城市、纬度、经度、时区、ISO代码、洲和子地区(使用MaxMind的GeoIP2数据库)
1.0.1
2016-12-03 11:37 UTC
Requires
- php: >=5.4.0
- maxmind-db/reader: ~1.0
- yiisoft/yii2: >=2.0.0
Requires (Dev)
- phpunit/phpunit: ~4.4
This package is auto-updated.
Last update: 2024-09-20 17:07:25 UTC
README
Result.php 中的子地区经度错误。
参见注释代码。
PHP Notice 'yii\base\ErrorException' with message 'Undefined index: de'
in /vendor/user1007017/yii2-geoip2/Result.php:89
Stack trace:
#0 /commands/ConsoleController.php(71): user1007017\GeoIP2\Result->__get()
#1 /vendor/yiisoft/yii2/base/InlineAction.php(57): app\commands\ConsoleController->actionIndex()
提供用户通过IP地址的地理位置信息。
目前可用
- 国家
- 城市
- 纬度,经度
- 时区
- ISO代码
- 洲
- 子地区
安装
$ php composer.phar require user1007017/yii2-geoip2 "~1.0.1"
或
将以下内容添加到您的 composer.json
{ "require": { "user1007017/yii2-geoip2": "~1.0.1" } }
并运行
$ composer update
使用方法
更新您的配置文件 - config/web.php
<?php $config = [ ... 'components' => [ 'geoip2' => [ 'class' => 'user1007017\GeoIP2\GeoIP2', 'mmdb' => '@app/components/GeoIP2/GeoLite2-City.mmdb', 'lng' => 'en', // available languages = 'de', 'en', 'es', 'ja', 'ru', 'zh-CN' ], ] ... ];
在代码的某处
$ip = Yii::$app->component->geoip2->getInfoByIP(); // current user ip $ip = Yii::$app->component->geoip2->getInfoByIP("8.8.8.8"); $ip->continent; // "North America" $ip->country; // "United States" $ip->isoCode; // "US" $ip->subdivisions; // "California" $ip->city; // "Mountain View" $ip->location->longitude; // -122.0838 $ip->location->latitude; // 37.386
本产品使用MaxMind创建的GeoLite2数据,可从以下网址获取:http://www.maxmind.com
在哪里可以下载Maxmind免费数据库?
从 http://dev.maxmind.com/geoip/geoip2/geolite2/
或
cd /usr/local/share/GeoIP (change to your folder) wget -N -q http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz gunzip -c GeoLite2-City.mmdb.gz > GeoLite2-City.mmdb