gpslab / geoip2
Maxmind GeoIP2 API 的 Symfony 扩展包
Requires
- php: >=7.2.0
- ext-phar: *
- ext-zlib: *
- geoip2/geoip2: ~2.0
- symfony/config: ~2.8|~3.0|~4.0|~5.0|~6.0|~7.0
- symfony/console: ~2.8|~3.0|~4.0|~5.0|~6.0|~7.0
- symfony/dependency-injection: ~2.8|~3.0|~4.0|~5.0|~6.0|~7.0
- symfony/expression-language: ~2.8|~3.0|~4.0|~5.0|~6.0|~7.0
- symfony/filesystem: ~2.8|~3.0|~4.0|~5.0|~6.0|~7.0
- symfony/http-kernel: ~2.8|~3.0|~4.0|~5.0|~6.0|~7.0
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpstan/phpstan-phpunit: ^1.3
- phpunit/phpunit: ~7.0|~8.0|~9.0
Suggests
- splitbrain/php-archive: Greatly reduces memory usage for the geoip2:update command
README
Maxmind GeoIP2 API 的 Symfony 扩展包
用于在 Symfony 中使用 maxmind/GeoIP2 的扩展包。
安装
使用 Composer 非常简单,运行
composer req gpslab/geoip2
配置
要配置自动更新数据库,您需要生成您个人的许可密钥。
生成许可密钥的步骤
- 注册 MaxMind 账户(无需购买)
- 登录并生成 许可密钥
- 保存您的许可密钥
- 打开 下载页面 并找到您需要的 DB 版本
ID
并复制第一列的值。
示例配置
gpslab_geoip: # Your personal licence key license: 'XXXXXXXXXXXXXXXX' # One of database edition IDs: # GeoLite2-ASN # GeoLite2-City # GeoLite2-Country # GeoIP2-City # GeoIP2-Country # GeoIP2-Anonymous-IP # GeoIP2-Domain # GeoIP2-ISP edition: 'GeoLite2-City'
数据库源 URL
默认情况下,此 URL 用于下载新的数据库 https://download.maxmind.com/app/geoip_download?edition_id={edition_id}&license_key={license_key}&suffix=tar.gz
您可以根据需要更改此 URL,例如,如果您想使用代理下载数据库。您可以在配置中自定义源 URL。
gpslab_geoip: license: 'XXXXXXXXXXXXXXXX' edition: 'GeoLite2-City' url: 'https://example.com/GeoLite2-City.tar.gz'
目标下载路径
默认情况下,新数据库下载到 %kernel.cache_dir%/{edition_id}.mmdb
,其中 edition_id
是来自 下载页面 第一列的字符 ID 名称。即默认情况下,新数据库将下载到 var/cache/{env}/
文件夹中。为每个环境保持数据库在缓存文件夹中可能不是最优的。您可以选择所有环境共享的目录。
gpslab_geoip: license: 'XXXXXXXXXXXXXXXX' edition: 'GeoLite2-City' path: '%kernel.project_dir%/var/GeoLite2-City.mmdb'
本地化
默认情况下,使用英语区域设置用于 GeoIP 记录。您可以更改记录的区域设置并声明多个区域设置作为后备。
gpslab_geoip: license: 'XXXXXXXXXXXXXXXX' edition: 'GeoLite2-City' locales: [ 'ru', 'en' ]
使用
您可以获取 GeoIP2 读取器服务
use GeoIp2\Database\Reader; // get a GeoIP2 reader $reader = $this->get(Reader::class); // or //$reader = $this->get('geoip2.reader'); // get a GeoIP2 City model $record = $reader->city('128.101.101.101'); 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
有关更多示例,请参阅 GeoIP2 库。
多个数据库
您可以在一个应用程序中使用多个 GeoIP 数据库。需要更新配置文件。
gpslab_geoip: databases: default: license: 'XXXXXXXXXXXXXXXX' edition: 'GeoLite2-City' country: license: 'XXXXXXXXXXXXXXXX' edition: 'GeoLite2-Country' asn: license: 'XXXXXXXXXXXXXXXX' edition: 'GeoLite2-ASN'
在应用程序中使用
// get a GeoIP2 reader for City database $default_reader = $this->get('geoip2.database.default_reader'); // or //$default_reader = $this->get(Reader::class); // or //$default_reader = $this->get('geoip2.reader'); // get a GeoIP2 reader for Country database $country_reader = $this->get('geoip2.database.country_reader'); // get a GeoIP2 reader for ASN database $asn_reader = $this->get('geoip2.database.asn_reader');
您可以重命名默认数据库。
gpslab_geoip: default_database: 'city' databases: asn: license: 'XXXXXXXXXXXXXXXX' edition: 'GeoLite2-ASN' city: license: 'XXXXXXXXXXXXXXXX' edition: 'GeoLite2-City' country: license: 'XXXXXXXXXXXXXXXX' edition: 'GeoLite2-Country'
// get a GeoIP2 reader for City database $default_reader = $this->get('geoip2.database.city_reader'); // or //$default_reader = $this->get(Reader::class); // or //$default_reader = $this->get('geoip2.reader');
为了不重复每个数据库的许可密钥和区域设置,您可以一次指定它们。
gpslab_geoip: license: 'XXXXXXXXXXXXXXXX' # global license locales: [ 'ru', 'en' ] # global locales default_database: 'city' databases: asn: edition: 'GeoLite2-ASN' locales: [ 'fr' ] # customize locales city: edition: 'GeoLite2-City' url: 'https://example.com/GeoLite2-City.tar.gz' # customize url path: '%kernel.project_dir%/var/GeoLite2-City.mmdb' # customize path country: edition: 'GeoLite2-Country' license: 'YYYYYYYYYYYYYYYY' # customize license
客户端区域设置的 GeoIP 数据
如果您想向用户显示 GeoIP 数据并以用户区域设置显示,则可以使用读取器工厂。
use GpsLab\Bundle\GeoIP2Bundle\Reader\ReaderFactory; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class GeoIPController { public function index(Request $request, ReaderFactory $factory): Response { $client_locale = $request->getLocale(); $client_ip = $request->getClientIp(); $database_name = 'default'; $fallback_locale = 'en'; $reader = $factory->create($database_name, [$client_locale, $fallback_locale]); $record = $reader->city($client_ip); return new Response(sprintf('You are from %s?', $record->country->name)); } }
控制台命令
更新 GeoIP 数据库
执行控制台命令以更新所有数据库
php bin/console geoip2:update
如果您使用多个数据库,则对于配置
gpslab_geoip: # ... databases: asn: # ... city: # ... country: # ...
您可以更新多个数据库
php bin/console geoip2:update city country
可选安装 splitbrain/php-archive 在更新数据库时使用更少的内存,并可以避免内存不足错误
composer req splitbrain/php-archive
下载 GeoIP 数据库
您可以使用控制台命令下载自定义数据库
php bin/console geoip2:download https://example.com/GeoLite2-City.tar.gz /path/to/GeoLite2-City.mmdb
许可协议
此软件包遵循MIT许可证。完整的许可证文件请参阅:LICENSE