klkvsk/maxmind-geoip-bundle

将 MaxMind GeoIP2 数据库集成到 symfony 应用程序中的包

安装: 3

依赖者: 0

建议者: 0

安全: 0

星星: 0

关注者: 0

分支: 18

类型:symfony-bundle

3.0.2 2024-02-07 20:43 UTC

This package is auto-updated.

Last update: 2024-09-07 21:51:28 UTC


README

注意:这是 https://github.com/cravler/CravlerMaxMindGeoIpBundle 的分支。增加了对 Symfony 7 的支持

安装

步骤 1:下载 Bundle

composer require klkvsk/maxmind-geoip-bundle

此命令需要您全局安装了 Composer,具体请参阅 Composer 文档中的 安装章节

步骤 2:启用 Bundle

此 Bundle 应该由 Flex 自动启用。如果您不使用 Flex,则需要手动通过在项目中的 config/bundles.php 文件中添加以下行来启用该 Bundle

<?php
// config/bundles.php

return [
    // ...
    Cravler\MaxMindGeoIpBundle\CravlerMaxMindGeoIpBundle::class => ['all' => true],
];

配置

该 Bundle 的默认配置如下

cravler_max_mind_geo_ip:
    client:
        user_id: ~
        license_key: ~
        options: {}
    path: '%kernel.project_dir%/resources/MaxMind'
    db:
        country: 'GeoIP2-Country.mmdb'
        city: 'GeoIP2-City.mmdb'
        asn: 'GeoIP2-ASN.mmdb'
        connection_type: 'GeoIP2-Connection-Type.mmdb'
        anonymous_ip: 'GeoIP2-Anonymous-IP.mmdb'
        enterprise: 'GeoIP2-Enterprise.mmdb'
        domain: 'GeoIP2-Domain.mmdb'
        isp: 'GeoIP2-ISP.mmdb'
    source:
        country: ~
        city: ~
        asn: ~
        connection_type: ~
        anonymous_ip: ~
        enterprise: ~
        domain: ~
        isp: ~
    md5_check:
        country: ~
        city: ~
        asn: ~
        connection_type: ~
        anonymous_ip: ~
        enterprise: ~
        domain: ~
        isp: ~

如果您需要 GeoLite2 许可证

  1. 注册 MaxMind 账户(无需购买)
  2. 设置您的密码并创建一个 许可证密钥 此密钥将用于 GeoIP 更新? > 否
# config/packages/cravler_max_mind_geo_ip.yaml

parameters:
    max_mind.license_key: '<YOUR_LICENSE_KEY>'

cravler_max_mind_geo_ip:
    source:
        country: 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&suffix=tar.gz&license_key=%max_mind.license_key%'
        city: 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&suffix=tar.gz&license_key=%max_mind.license_key%'
        asn: 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&suffix=tar.gz&license_key=%max_mind.license_key%'
    md5_check:
        country: 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&suffix=tar.gz.md5&license_key=%max_mind.license_key%'
        city: 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&suffix=tar.gz.md5&license_key=%max_mind.license_key%'
        asn: 'https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN&suffix=tar.gz.md5&license_key=%max_mind.license_key%'

如果您已经购买了许可证

# config/packages/cravler_max_mind_geo_ip.yaml

parameters:
    max_mind.user_id: '<YOUR_USER_ID>'
    max_mind.license_key: '<YOUR_LICENSE_KEY>'

cravler_max_mind_geo_ip:
    client:
        user_id: '%max_mind.user_id%'
        license_key: '%max_mind.license_key%'
        ...
    source:
        country: 'https://download.maxmind.com/app/geoip_download?edition_id=GeoIP2-Country&suffix=tar.gz&license_key=%max_mind.license_key%'
        ...
    md5_check:
        country: ~
        ...

注意! 请勿忘记更改您的许可证数据。

下载并更新 MaxMind GeoIp2 数据库

php bin/console cravler:maxmind:geoip-update

如果您想跳过 MD5 检查,可以使用 --no-md5-check 选项。

php bin/console cravler:maxmind:geoip-update --no-md5-check

如何使用

数据库读取器

$geoIpService = $container->get('cravler_max_mind_geo_ip.service.geo_ip_service');

// Replace "city" with the appropriate method for your database, e.g., "country".
$record = $geoIpService->getRecord('128.101.101.101', 'city');

print($record->country->isoCode . "\n"); // 'US'
print($record->country->name . "\n"); // 'United States'
print($record->city->name . "\n"); // 'Minneapolis'

网络服务客户端

$geoIpService = $container->get('cravler_max_mind_geo_ip.service.geo_ip_service');

$client = $geoIpService->getClient();

$record = $client->city('128.101.101.101');

许可证

此 Bundle 采用 MIT 许可证。请参阅包中的完整许可证。

LICENSE