gupalo / geoip
GeoIP 客户端
1.3.2
2022-03-17 10:16 UTC
Requires
- php: ^8.0
- maxmind-db/reader: ^1.9
Requires (Dev)
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.5
- twig/twig: ^3.2
README
MaxMind 和 Sypex GeoIP 数据库客户端的适配器。
用法
composer require gupalo/geoip
将 GeoIP 数据库放入某个目录。支持的数据库
- GeoIP2-City.mmdb 或 GeoLite2-City.mmdb
- GeoIP2-Country.mmdb 或 GeoLite2-Country.mmdb
- GeoIP2-Domain.mmdb
- GeoIP2-ISP.mmdb
- SxGeoMax.dat
您可以下载 MaxMind 轻量级数据库
export MAX_MIND_LICENSE_KEY="XXXXXXXXXXXXXXXX" # you need to register to get the key
export TARGET_DIR="/opt/geoip" # or any other folder where you keep geoip files
curl "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-Country&suffix=tar.gz&license_key=${MAX_MIND_LICENSE_KEY}" | tar zxf - -C ${TARGET_DIR}/ --strip-components=1 --no-anchored --wildcards *.mmdb
curl "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&suffix=tar.gz&license_key=${MAX_MIND_LICENSE_KEY}" | tar zxf - -C ${TARGET_DIR}/ --strip-components=1 --no-anchored --wildcards *.mmdb
用法
$parser = new GeoIpParser($dir);
$geoIp = $parser->parse('1.1.1.1');
$ip = $geoip->getIp();
$isValidIp = $geoip->isValidIp();
$city = $geoip->getCity();
$countryCode = $geoip->getCountryCode();
$country = $geoip->getCountry();
$continentCode = $geoip->getContinentCode();
$postalCode = $geoip->getPostalCode();
$domain = $geoip->getDomain();
$asnNumber = $geoip->getAsnNumber();
$asnOrganization = $geoip->getAsnOrganization();
$isp = $geoip->getIsp();
$organization = $geoip->getOrganization();
$latitude = $geoip->getLatitude();
$longitude = $geoip->getLongitude();
$region = $geoip->getRegion();
$timezone = $geoip->getTimezone();
$currencyCode = $geoip->getCurrencyCode();
$cityPopulation = $geoip->getCityPopulation();
$cityTel = $geoip->getCityTel();
$regionAuto = $geoip->getRegionAuto();
$countryArea = $geoip->getCountryArea();
$countryPopulation = $geoip->getCountryPopulation();
$countryCapital = $geoip->getCountryCapital();
$countryPhoneCode = $geoip->getCountryPhoneCode();
高级用法
如果数据可能存在于多个数据库中,例如纬度/经度,则从最佳数据库中获取数据
- MaxMind 城市(如果不可用 - MaxMind 城市 Lite)
- Sypex(如果有城市数据)
- MaxMind 国家(如果不可用 - MaxMind 国家 Lite)
- Sypex
您可以获取特定数据库的数据
$parser = new GeoIpParser($dir);
$geoIp = $parser->parseAdvanced('1.1.1.1');
$latitudes = [
$geoIp->getSypexCityLatitude(),
$geoIp->getSypexRegionLatitude(),
$geoIp->getSypexCountryLatitude(),
$geoIp->getMaxMindCityLatitude(),
];
您还可以从以下获取原始数据
$parser = new GeoIpParser($dir);
$geoIp = $parser->parseAdvanced('1.1.1.1');
$raw = [
$geoIp->getSypexCityRaw(),
$geoIp->getMaxMindCountryRaw(),
$geoIp->getMaxMindCityRaw(),
$geoIp->getMaxMindDomainRaw(),
$geoIp->getMaxMindIspRaw(),
];
Symfony
添加到 config/services.yaml
parameters:
...
env(GEOIP_DIR): '%kernel.project_dir%/data/geoip'
services:
...
Gupalo\GeoIp\GeoIpParser:
arguments: ['%env(resolve:GEOIP_DIR)%']
Gupalo\GeoIp\Twig\GeoIpExtension:
tags: ['twig.extension']
使用自动装配
/**
* @Route("/test", name="test")
*/
public function test(GeoIpParser $geoIpParser): Response
{
dd($geoIpParser->parse('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_country
:将 IP 转换为国家名称 -'1.1.1.1'|ip_country
- 澳大利亚,俄罗斯,...ip_flag
:将 IP 转换为带有标志的 HTML -'1.1.1.1'|ip_flag
country_code_flag
:将国家代码转换为带有标志的 HTML -'RU'|country_code_flag
要使用标志,请将 public/css/flags.css
和 public/img/flags.png
复制到您的 public 文件夹。
添加到 base.html.twig
或其他模板
<link rel="stylesheet" href="{{ asset('css/flags.css') }}">