cravler/maxmind-geoip-bundle

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

安装次数: 535,817

依赖项: 2

建议者: 0

安全性: 0

星标: 25

关注者: 2

分支: 18

公开问题: 0

类型:symfony-bundle

v2.0.0 2021-12-01 12:25 UTC

This package is auto-updated.

Last update: 2024-09-23 10:18:06 UTC


README

安装

步骤 1: 下载软件包

composer require cravler/maxmind-geoip-bundle:3.x-dev

此命令要求您全局安装 Composer,如 Composer 文档中的安装章节所述。

步骤 2: 启用软件包

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

<?php
// config/bundles.php

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

配置

软件包的默认配置如下所示

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');

许可证

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

LICENSE