reefki/laravel-geoip
Composer 安装命令:
composer require reefki/laravel-geoip
包简介
A Laravel package to get IP addresses geographical location.
README 文档
README
A Laravel package to get geographical location information from IP addresses.
Requirements
- PHP 8.1 or higher
- Laravel 10, 11, or 12
Installation
Install the package via Composer:
composer require reefki/laravel-geoip
Optionally, publish the config file:
php artisan vendor:publish --provider="Reefki\Geoip\GeoipServiceProvider" --tag="config"
Usage
Using the Facade
use Reefki\Geoip\Geoip; $geoip = Geoip::get('8.8.8.8'); $geoip->driver; // geojs $geoip->ip; // 8.8.8.8 $geoip->city; // Mountain View $geoip->region; // California $geoip->country; // United States $geoip->country_code; // US $geoip->continent_code; // NA $geoip->timezone; // America/Los_Angeles $geoip->latitude; // 37.751 $geoip->longitude; // -97.822 $geoip->cached; // true or false
IPv6 addresses are also supported:
$geoip = Geoip::get('2001:4860:4860::8888');
Disabling Cache
By default, results are cached. To get realtime data:
Geoip::get('8.8.8.8', cache: false);
Using with Request
Get GeoIP data for the current request's IP address:
$geoip = $request->geoip();
With anonymized IP (for GDPR compliance):
$geoip = $request->geoip(anonymize: true);
Get just the anonymized IP address:
$anonymizedIp = $request->anonymizedIp(); // 8.8.8.8 → 8.8.8.0 // 2001:4860:4860::8888 → 2001:4860:4860::
Configuration
Default Driver
Set the default driver in your .env file:
GEOIP_DEFAULT_DRIVER=geojs
Cache Settings
Configure caching behavior:
GEOIP_CACHE_STORE=redis
GEOIP_CACHE_TTL=86400
HTTP Settings
Configure timeout and retry for API requests:
GEOIP_TIMEOUT=10
GEOIP_RETRY=3
Drivers
GeoJS (Default)
GeoJS is a free service with no API key required.
Geoip::driver('geojs')->get('8.8.8.8');
IPData
IPData requires an API key. Register for an account and add your key to .env:
IPDATA_API_KEY=your_api_key
Then use the driver:
Geoip::driver('ip-data')->get('8.8.8.8');
Note: IPData offers 1,500 free requests per day. Higher usage requires a paid plan.
Testing
vendor/bin/phpunit
Credits
License
The MIT License (MIT). Please see License File for more information.
reefki/laravel-geoip 适用场景与选型建议
reefki/laravel-geoip 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 647 次下载、GitHub Stars 达 21, 最近一次更新时间为 2023 年 10 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 reefki/laravel-geoip 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 reefki/laravel-geoip 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 647
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 21
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-08