scorpsan/ yii2-geoip
一个扩展,允许您从任何IP地址获取访客的IP地址和位置信息。使用 http://sypexgeo.net/ 进行在线数据检索,使用 http://www.ip2location.com/ 库进行离线数据检索。
v1.3.3
2023-04-21 07:07 UTC
Requires
- guzzlehttp/guzzle: ^7.2
- ip2location/ip2location-php: ^9.0
- yiisoft/yii2: *
README
一个扩展,允许您从任何IP地址获取访客的位置信息。使用 http://sypexgeo.net/ 进行在线数据检索,使用 http://www.ip2location.com/ 库进行离线数据检索。
扩展,允许您从任何IP地址获取访客的位置信息。使用 http://sypexgeo.net/ 进行在线数据检索,使用 http://www.ip2location.com/ 库进行离线数据检索。
安装
建议通过 composer 安装此扩展。
您可以在控制台中设置
$ composer require "scorpsan/yii2-geoip"
或添加
"scorpsan/yii2-geoip": "*"
在 composer.json
文件的 require
部分中。
使用
扩展安装后,只需在您的代码中使用它即可
将以下代码添加到您的应用程序配置文件中
... 'components' => [ ... 'geoIp' => [ 'class' => 'scorpsan\geoip\GeoIp', // uncomment next line if you register on sypexgeo.net and paste your key // 'keySypex' => 'key-sypexgeo-net-this', // if need more timeout (default 5 = 5000 millisecond) // 'timeout' => 6, ], ... ], ...
从Ip用户获取信息:在线
var_dump(Yii::$app->geoIp->info);
离线
var_dump(Yii::$app->geoIp->infoDb);
从选择Ip获取信息:在线
var_dump(Yii::$app->geoIp->getInfo('255.255.255.255'));
离线
var_dump(Yii::$app->geoIp->getInfoDb('255.255.255.255'));
获取用户Ip
var_dump(Yii::$app->geoIp->ip);
返回数据
在线
* Returned information by IP address with following paramters: * - `ip` - Visitor IP address, or IP address specified as parameter. * - `city` - Object City information * -- [id] => 625144 * -- [lat] => 53.9 * -- [lon] => 27.56667 * -- [name_ru] => Минск * -- [name_en] => Minsk * -- [name_de] => Minsk * -- [name_fr] => Minsk * -- [name_it] => Minsk * -- [name_es] => Minsk * -- [name_pt] => Minsk * -- [okato] => 5000000000 * -- [vk] => 282 * -- [population] => 1742124 * - `region` - Object Region information * -- [id] => 625143 * -- [lat] => 53.9 * -- [lon] => 27.57 * -- [name_ru] => Минск * -- [name_en] => Horad Minsk * -- [name_de] => Minsk * -- [name_fr] => Minsk * -- [name_it] => Minsk * -- [name_es] => Minsk * -- [name_pt] => Minsk * -- [iso] => BY-HM * -- [timezone] => Europe/Minsk * -- [okato] => 5 * -- [auto] => 7 * -- [vk] => 0 * -- [utc] => 3 * - `country` - Object Country information * -- [id] => 36 * -- [iso] => BY * -- [continent] => EU * -- [lat] => 53 * -- [lon] => 28 * -- [name_ru] => Беларусь * -- [name_en] => Belarus * -- [name_de] => Weißrussland * -- [name_fr] => Biélorussie * -- [name_it] => Bielorussia * -- [name_es] => Bielorrusia * -- [name_pt] => Bielorrússia * -- [timezone] => Europe/Minsk * -- [area] => 207600 * -- [population] => 9685000 * -- [capital_id] => 625144 * -- [capital_ru] => Минск * -- [capital_en] => Minsk * -- [cur_code] => BYR * -- [phone] => 375 * -- [neighbours] => PL,LT,UA,RU,LV * -- [vk] => 3 * -- [utc] => 3 * - `error` - Error data. * - `request` - Request code. * - `created` - Date created info in database. * - `timestamp` - Timestamp request. * * @return array|false */
离线
* Returned information by IP address with following paramters: * - `ipAddress` - Visitor IP address, or IP address specified as parameter. * - `countryName` - Name Country in English. * - `countryCode` - Two-letter ISO 3166-1 alpha-2 country code. * * @return array|false */
使用会话以最小化对Sypexgeo的请求
PHP控制器(例如)
/** Get User Location */ //unset($session['_location']); if (isset($session['_location']) && !empty($session['_location']['userCountry'])) { $app->params['userCountry'] = $session['_location']['userCountry']; $app->params['userCountryCode'] = $session['_location']['userCountryCode']; $app->params['userCountryRegion'] = $session['_location']['userCountryRegion']; $app->params['userCountryCity'] = $session['_location']['userCountryCity']; $app->params['userPhoneCode'] = $session['_location']['userPhoneCode']; } else { //$geoip = $app->geoip->getInfo('93.176.236.137'); $geoip = $app->geoip->info; if (isset($geoip->ip)) : $app->params['userCountry'] = $geoip->country->name_en; $app->params['userCountryCode'] = $geoip->country->iso; $app->params['userCountryRegion'] = $geoip->region->name_en; $app->params['userCountryCity'] = $geoip->city->name_en; $app->params['userPhoneCode'] = $geoip->country->phone; else : $geoip = $app->geoip->infoDb; if (isset($geoip['ipAddress'])) : $app->params['userCountry'] = $geoip['countryName']; $app->params['userCountryCode'] = $geoip['countryCode']; $app->params['userCountryRegion'] = $geoip['regionName']; $app->params['userCountryCity'] = $geoip['cityName']; $app->params['userPhoneCode'] = $geoip['iddCode']; endif; endif; $session['_location'] = [ 'userCountry' => $app->params['userCountry'], 'userCountryCode' => $app->params['userCountryCode'], 'userCountryRegion' => $app->params['userCountryRegion'], 'userCountryCity' => $app->params['userCountryCity'], 'userPhoneCode' => $app->params['userPhoneCode'], ]; }
params.php
return [ ... 'userCountry' => 'World', 'userCountryCode' => 'wl', 'userCountryRegion' => '', 'userCountryCity' => '', 'userPhoneCode' => '1', ... ];
许可协议
yii2-geoip 在BSD 3-Clause License下发布。