Yii 2.0: yii2-ip2location

This extension works with IP2Location on Yii2 Framework.
3 followers

This extension provides country, region, city, latitude, longitude, ZIP code, time zone, ISP, domain name, connection speed, IDD code, area code, weather station code, weather station name, MNC, MCC, mobile brand, elevation, and usage type from IP address by using IP2Location database. This module uses a file based database available at IP2Location.com

Installation

  • Download
  • Extract this package to frontend/components

Composer installation is coming soon.

Quick Start

Use the following methods to retrieve geolocation information. * Add following lines into main.php configuration file:

'components' => [
     'ip2location' => [
        'class' => '\frontend\components\IP2Location\Geolocation',
        'database' => __DIR__ .DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'components'.DIRECTORY_SEPARATOR.'IP2Location'.DIRECTORY_SEPARATOR.'IP2LOCATION-LITE-DB1.BIN',
        'mode' => 'FILE_IO',
    ],
]

Usage

$ip = Yii::$app->request->userIP
$countryCode = Yii::$app->ip2location->getCountryCode($ip);
$countryName = Yii::$app->ip2location->getCountryName($ip);
$regionName = Yii::$app->ip2location->getRegionName($ip);
$cityName = Yii::$app->ip2location->getCityName($ip);
$latitude = Yii::$app->ip2location->getLatitude($ip);
$longitude = Yii::$app->ip2location->getLongitude($ip);
.... And so on

Key Notes

You must enable

php_gmp

extension before using it.

Database Update

IP2Location database is updated monthly. You can get the latest database from http://www.ip2location.com (Commercial version) or http://lite.ip2location.com (Free version).

Resources

Be the first person to leave a comment

Please to leave your comment.

Create extension