duylangbk / lumen-geoip
Lumen的GeoIP
v1.2
2019-12-16 07:21 UTC
Requires
- php: >=5.5.9
- geoip2/geoip2: ^2.4
This package is auto-updated.
Last update: 2024-09-16 18:02:42 UTC
README
根据网站访客的IP地址确定其地理位置。
安装
要安装此包,只需通过composer安装即可
$ composer require duylangbk/lumen-geoip
提供者
接下来,打开 bootstrap/app.php 并在注册服务提供者部分下方添加
... $app->register(Codenexus\GeoIP\GeoIPServiceProvider::class);
更新MaxMind GeoLite2城市数据库
从项目的根目录运行此命令行
$ php artisan geoip:update
用法
GeoIP将尝试按以下顺序使用以下http头确定IP:HTTP_CLIENT_IP、HTTP_X_FORWARDED_FOR、HTTP_X_FORWARDED、HTTP_X_CLUSTER_CLIENT_IP、HTTP_FORWARDED_FOR、HTTP_FORWARDED、REMOTE_ADDR。您可以设置IP作为唯一参数来设置它。
$record = app()->geoip->getLocation('232.223.11.11'); $record = GeoIP::getLocation('232.223.11.11'); // If you have enabled facades print($record->country->isoCode . "\n"); // 'US' print($record->country->name . "\n"); // 'United States' print($record->country->names['zh-CN'] . "\n"); // '美国' print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota' print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN' print($record->city->name . "\n"); // 'Minneapolis' print($record->postal->code . "\n"); // '55455' print($record->location->latitude . "\n"); // 44.9733 print($record->location->longitude . "\n"); // -93.2323
其他方法
这些方法也适用于您的应用程序。
app()->geoip->checkIp($ip) // Checks IP to make sure IP is a valid IPv4 or IPv6 address and not within a private or reserved range app()->geoip->getIp() // Returns the detected client IP
默认位置数据
当IP未检测到时,它将被设置为127.0.0.1,最终将抛出异常。如果您不在生产环境中,则记录将默认为以下数据。
array ( "ip" => "232.223.11.11", "isoCode" => "US", "country" => "United States", "city" => "New Haven", "state" => "CT", "postal_code" => "06510", "lat" => 41.28, "lon" => -72.88, "timezone" => "America/New_York", "continent" => "NA", "default" => false );
变更日志
v2.0.0
- 简化命名空间
- 添加了外观支持
- 在开发模式下添加了默认位置
- 修复了检测到的IP始终为空的错误