overals/yii2-geoip2

Yii2 GeoIP2 扩展。通过IP地址返回国家、城市、纬度、经度、时区、ISO代码、大洲、行政区划(使用MaxMind的GeoIP2数据库)

安装量: 5,693

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 2

分支: 2

公开问题: 0

类型:yii2-extension

1.0.2 2018-04-12 15:44 UTC

This package is auto-updated.

Last update: 2024-09-14 03:43:20 UTC


README

提供用户IP地址的地理位置信息。

目前可用

  • 国家
  • 城市
  • 纬度,经度
  • 时区
  • ISO代码
  • 大洲
  • 行政区划

安装

$ php composer.phar require overals/yii2-geoip2 "~1.0.1"

或者

将以下内容添加到您的 composer.json

{
    "require": {
        "overals/yii2-geoip2": "~1.0.1"
    }
}

并运行

$ composer update

使用方法

更新您的配置文件 - config/web.php

<?php

$config = [
    ...
    'components' => [
        'geoip2' => [
            'class' => 'overals\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