jmiy / geoip
multiple GeoIP services for Hyperf
dev-master / 1.0.x-dev
2021-02-07 08:24 UTC
Requires
- php: >=7.2
- hyperf/di: ^2.0
- hyperf/utils: ^2.0
- nesbot/carbon: ^2.0
- psr/simple-cache: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.14
- geoip2/geoip2: ~2.1
- hyperf/config: ^2.0
- hyperf/redis: ^2.0
- mockery/mockery: ^1.0
- phpstan/phpstan: ^0.12
- phpunit/phpunit: >=7.0
- swoole/ide-helper: dev-master
- swow/swow: dev-develop
- symfony/var-dumper: ^5.1
- vlucas/phpdotenv: ^3.5
Suggests
- geoip2/geoip2: Required to use the MaxMind database or web service with GeoIP (~2.1).
- monolog/monolog: Allows for storing location not found errors to the log
Replaces
- torann/geoip: *
This package is auto-updated.
Last update: 2024-11-07 17:32:38 UTC
README
安装
composer require jmiy/geoip
配置
创建配置文件
php bin/hyperf.php vendor:publish jmiy/geoip
配置如下
<?php declare(strict_types=1); return [ /* |-------------------------------------------------------------------------- | Logging Configuration |-------------------------------------------------------------------------- | | Here you may configure the log settings for when a location is not found | for the IP provided. | */ 'log_failures' => true, /* |-------------------------------------------------------------------------- | Include Currency in Results |-------------------------------------------------------------------------- | | When enabled the system will do it's best in deciding the user's currency | by matching their ISO code to a preset list of currencies. | */ 'include_currency' => true, /* |-------------------------------------------------------------------------- | Default Service |-------------------------------------------------------------------------- | | Here you may specify the default storage driver that should be used | by the framework. | | Supported: "maxmind_database", "maxmind_api", "ipapi" | */ 'service' => 'ipapi', /* |-------------------------------------------------------------------------- | Storage Specific Configuration |-------------------------------------------------------------------------- | | Here you may configure as many storage drivers as you wish. | */ 'services' => [ 'maxmind_database' => [ 'class' => \Torann\GeoIP\Services\MaxMindDatabase::class, 'database_path' => BASE_PATH . '/storage/app/geoip.mmdb', 'update_url' => sprintf('https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=%s&suffix=tar.gz', env('MAXMIND_LICENSE_KEY')), 'locales' => ['en'], ], 'maxmind_api' => [ 'class' => \Torann\GeoIP\Services\MaxMindWebService::class, 'user_id' => env('MAXMIND_USER_ID'), 'license_key' => env('MAXMIND_LICENSE_KEY'), 'locales' => ['en'], ], 'ipapi' => [ 'class' => \Torann\GeoIP\Services\IPApi::class, 'secure' => true, 'key' => env('IPAPI_KEY'), 'continent_path' => BASE_PATH . '/storage/app/continents.json', 'lang' => 'en', ], 'ipgeolocation' => [ 'class' => \Torann\GeoIP\Services\IPGeoLocation::class, 'secure' => true, 'key' => env('IPGEOLOCATION_KEY'), 'continent_path' => BASE_PATH . '/storage/app/continents.json', 'lang' => 'en', ], 'ipdata' => [ 'class' => \Torann\GeoIP\Services\IPData::class, 'key' => env('IPDATA_API_KEY'), 'secure' => true, ], 'ipfinder' => [ 'class' => \Torann\GeoIP\Services\IPFinder::class, 'key' => env('IPFINDER_API_KEY'), 'secure' => true, 'locales' => ['en'], ], ], /* |-------------------------------------------------------------------------- | Default Cache Driver |-------------------------------------------------------------------------- | | Here you may specify the type of caching that should be used | by the package. | | Options: | | all - All location are cached | some - Cache only the requesting user | none - Disable cached | */ 'cache' => 'all', /* |-------------------------------------------------------------------------- | Cache Tags |-------------------------------------------------------------------------- | | Cache tags are not supported when using the file or database cache | drivers in Laravel. This is done so that only locations can be cleared. | */ 'cache_tags' => ['torann-geoip-location'], /* |-------------------------------------------------------------------------- | Cache Expiration |-------------------------------------------------------------------------- | | Define how long cached location are valid. | */ 'cache_expires' => 30, /* |-------------------------------------------------------------------------- | Default Location |-------------------------------------------------------------------------- | | Return when a location is not found. | */ 'default_location' => [ 'ip' => '127.0.0.0', 'iso_code' => 'US', 'country' => 'United States', 'city' => 'New Haven', 'state' => 'CT', 'state_name' => 'Connecticut', 'postal_code' => '06510', 'lat' => 41.31, 'lon' => -72.92, 'timezone' => 'America/New_York', 'continent' => 'NA', 'default' => true, 'currency' => 'USD', ], ];
使用
助手函数
本组件实现了与 torann/geoip 一模一样的助手函数,可以按照以下方式
使用
torann/geoip 使用起来非常方便,它已经提供了辅助方法和 Facade:
<?php //上面两种方式效果相同,都会根据传入的 IP 返回 \Torann\GeoIP\Location 对象。这个对象包含了对应的位置信息。 geoip($ip); GeoIp::getLocation($ip);
Determine the geographical location and currency of website visitors based on their IP addresses.
Contributions
Many people have contributed to project since its inception.
Thanks to: