playtini / geoip
适用于 MaxMind 和 IP2Location 数据库的简单 GeoIP 客户端
1.1.5
2024-06-27 21:19 UTC
Requires
- php: ^8.1
- geoip2/geoip2: ^3.0
- ip2location/ip2location-php: ^9.7
- symfony/http-client: ^6.0|^7.0
- symfony/http-foundation: ^6.0|^7.0
- twig/twig: ^3.10
Requires (Dev)
- phpspec/prophecy-phpunit: ^2.2
- phpunit/phpunit: ^10.5
README
维护者
该库由 Playtini 创建并支持。
我们正在招聘市场营销人员(FB,Tiktok,UAC,应用内,Google)和开发者(PHP,JS):playtini.ua/jobs
安装
composer require playtini/geoip
使用
参见 examples/*.php
。
use Playtini\GeoIp\GeoIp; require_once(__DIR__ . '/vendor/autoload.php'); $geoIp = new GeoIp(); echo $geoIp->country('8.8.8.8') . "\n"; // US print_r($geoIp->city('190.0.0.1')); /* [country] => CO [city] => Medellín [subdivision] => Antioquia [subdivision1] => Antioquia [subdivision2] => [subdivision3] => [subdivision_code] => ANT [postal] => 050021 [accuracy_radius] => 10 [latitude] => 6.2529 [longitude] => -75.5646 [timezone] => America/Bogota */ echo $geoIp->maxMindCountry('190.0.0.1') . "\n"; // CO print_r($geoIp->maxMindCity('190.0.0.1')); /* [country] => CO [city] => Medellín [subdivision] => Antioquia [subdivision1] => Antioquia [subdivision2] => [subdivision3] => [subdivision_code] => ANT [postal] => 050021 [accuracy_radius] => 10 [latitude] => 6.2529 [longitude] => -75.5646 [timezone] => America/Bogota */ print_r($geoIp->maxMindAsn('190.0.0.1')); /* [org] => EPM Telecomunicaciones S.A. E.S.P. [num] => 13489 [net] => 190.0.0.0/18 */ echo $geoIp->ip2LocationCountry('190.0.0.1') . "\n"; // CO echo $geoIp->ip2LocationProxyType('190.0.0.1') . "\n"; // - echo $geoIp->ip2LocationProxyType('1.2.3.4') . "\n"; // VPN echo $geoIp->ip2LocationProxyType('8.8.8.8') . "\n"; // DCH
如果你的 GeoIP 文件不是默认命名或者不在 /usr/share/GeoIP/
目录下,则向构造函数传递参数。
优先级
对于方法 country
和 city
,IP2Location 数据库具有优先权
- country - 如果 IP2Location 数据不为空,则不查找 MaxMind
- city - 如果 IP2Location 国家与 MaxMind 不同,则使用 IP2Location 国家和空城市
如果您猜测 IP 可能来自哪些国家,则可以传递第二个参数 array $preferredCountries
。然后即使 IP2Location 国家与 MaxMind 不同,但 MaxMind 返回的是首选国家,则使用 MaxMind 国家。
echo $geoIp->maxMindCountry('80.231.192.1') . "\n"; // CA echo $geoIp->ip2LocationCountry('80.231.192.1') . "\n"; // DZ echo $geoIp->country('80.231.192.1') . "\n"; // DZ - IP2Location has higher priority echo $geoIp->country('80.231.192.1', ['BR', 'MX']) . "\n"; // DZ // IP2Location still has higher priority, MaxMind country is not among preferred countries echo $geoIp->country('80.231.192.1', ['CA', 'AU']) . "\n"; // CA // MaxMind country is among preferred countries; it is used instead of different IP2Location country
Symfony
添加到 config/services.yaml
imports: - { resource: '../vendor/playtini/geoip/config/config.yaml' }
您可以在不导入的情况下复制配置到您的 .yaml 文件中,并根据您的需求进行调整。
添加环境变量 GEOIP_DIR
,包含所有您的 GeoIP 数据库文件。默认 - %kernel.project_dir%/data/geoip
使用自动注入
/**
* @Route("/test", name="test")
*/
public function test(GeoIp $geoIp): Response
{
dd($geoIpParser->country('1.1.1.1'));
}
GeoIpExtension
可选添加,但如果添加了它,您将拥有 Twig 过滤器
domain_ip
:将域名或 IP 转换为 IP -'google.com'|domain_ip
,'1.1.1.1'|domain_ip
ip_country_code
:将 IP 转换为国家代码 -'1.1.1.1'|ip_country_code
- US,AU,...ip_flag
:将 IP 转换为带标志的 HTML -'1.1.1.1'|ip_flag
country_code_flag
:将国家代码转换为带标志的 HTML -'CA'|country_code_flag
要使用标志,请将 public/css/flags.css
和 public/img/flags.png
复制到您的公共文件夹。
添加到 base.html.twig
或其他模板
<link rel="stylesheet" href="{{ asset('css/flags.css') }}">