user1007017/yii2-geoip2

Yii2 GeoIP2 extension. Returns country, city, latitude, longitude, time zone, iso code, continent, subdivisions by IP (uses MaxMind's GeoIP2 databases)

Installs: 6

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 2

Type:yii2-extension

1.0.1 2016-12-03 11:37 UTC

This package is auto-updated.

Last update: 2024-11-20 17:30:31 UTC


README

Subdivisions lng error in Result.php.

See commented code.

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()

Provides information about geographical location of user by IP address.

Currently available:

  • Country
  • City
  • Latitude, Longitude
  • Time Zone
  • ISO Code
  • Continent
  • Subdivisions

Installation

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

OR

Add to your composer.json

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

and run

$ composer update

Usage

Update your config file - 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'
        ],
    ]
    ...
];

somewhere in code:

$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

This product uses GeoLite2 data created by MaxMind, available from http://www.maxmind.com

Where you can download Maxmind Free DBs?

From http://dev.maxmind.com/geoip/geoip2/geolite2/

OR

    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