The preferred way to install the geoip is through composer.
Either run
composer require sablesoft/yii2-geoip
or add
"sablesoft/yii2-geoip": "*",
to the require section of your composer.json.
Then add geoip in your app config:
'components' => [
'geoip' => [
'class' => 'sablesoft\geoip\GeoIP',
'dbPath' => '/path/to/your/geoip/database'
]
]
Use get
method to get IP data:
$ip = "52.141.159.163";
/** @var \sablesoft\geoip\IpData $ipData */
$ipData = \Yii::$app->geoip->get($ip);
$country = $ipData->countryName;
$city = $ipData->cityName;
$code = $ipData->countryCode;
$default = false;
$path = "some.path.in.ip.data.array";
$someData = $ipData->get($path, $default);