jetcod / ip-intelligence
一个用于定位IP地址并从中获取地域和语言信息的PHP包。
Requires
- php: ^7.3|^8.0
- ext-intl: *
- geoip2/geoip2: ^2.13
- laravel/framework: >6.0
- symfony/dotenv: ^5.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- mockery/mockery: ^1.5
- phpstan/phpstan: ^1.10
- phpstan/phpstan-phpunit: ^1.3
- phpunit/phpunit: ^9.6
Suggests
- symfony/filesystem: Required to handle filesystem errors (>5.4)
This package is not auto-updated.
Last update: 2024-09-20 07:32:46 UTC
README
概述
IP-Intelligence是一个多功能的PHP库,专为全面分析IP地址智能设计。它由强大的MaxMind数据库支持,并可与CLDR(通用地域数据存储库)包无缝集成,该库使您能够从IP地址中提取大量信息。无论您需要挖掘地理位置数据、ASN(自治系统编号)信息,还是检测与IP地址关联的语言和地域,IP-Intelligence都提供了丰富数据分析和决策过程所必需的工具。
要求
此库需要
- php7.4或8.0+
- Maxmind Db(可在MaxMind网站上获取 MaxMind 网站)
安装Maxmind Db后,您将拥有以下3个数据库文件:
- GeoLite2-Country.mmdb
- GeoLite2-City.mmdb
- GeoLite2-ASN.mmdb
请保存每个文件的路径以供后续安装。
有关安装程序和要求的综合指南,请参阅文档网站。
安装指南
步骤1:Composer安装
首先通过Composer安装IP-Intelligence包。在您的终端运行以下命令:
composer require jetcod/ip-intelligence
这将获取并安装必要的包文件。
步骤2:配置
IP-Intelligence需要配置才能有效运行。此库为Laravel项目提供定制的Artisan命令,简化了必要数据库的安装和配置。要启动此过程,只需执行以下Artisan命令:
php artisan IpIntelligence:data-install
在此设置过程中,您将被提示指定Maxmind数据库的路径,并且相关环境变量将自动配置。
如果您的项目不在Laravel框架内,您应该通过执行以下命令来集成cldr-core
包:
npm install cldr-core
随后,请确保在您的.env
文件中定义这些变量
用法
IP查找
此库的主要功能是执行IP地址查找。您可以使用ip()方法获取有关IP地址的各种详细信息。以下是如何使用它的示例:
<?php namespace App\Http\Controllers; use Jetcod\IpIntelligence\GeoIpLookup; class TestController { public function __invoke(GeoIpLookup $lookup) { $address = '206.47.249.128'; try { $ip = $lookup->ip($address); // Retrieve Language Information $officialLanguages = $ip->language()->officials(); // Returns an array of official languages spoken in the region, e.g., ['en', 'fr'] $allLanguages = $ip->language()->all(); // Returns an array of all languages spoken in the region, e.g., ['ar', 'atj', 'bla', 'bn', ...] $locale = $ip->language()->locale; // Returns the locale for the region, e.g., 'en_CA' // Retrieve City Information $cityName = $ip->city()->name; // Returns the name of the city, e.g., 'Toronto' // Retrieve Country Information $countryName = $ip->country()->name; // Returns the name of the country, e.g., 'Canada' dd( "IP Address: $address", "Official Languages: " . implode(', ', $officialLanguages), "All Languages: " . implode(', ', $allLanguages), "Locale: $locale", "City Name: $cityName", "Country Name: $countryName" ); } catch (\Jetcod\IpIntelligence\Exceptions\InvalidIpAddressException $e) { // Handle the case where the provided IP address is invalid. echo $e->getMessage(); } catch (\GeoIp2\Exception\AddressNotFoundException $e) { // Handle the case where the IP address is not found in the database. echo $e->getMessage(); } } }
此代码演示了如何初始化IpLookup类、执行IP查找并检索有关指定IP地址的信息,包括语言、城市和国家细节。请确保像代码中那样处理异常,以优雅地处理查找过程中可能出现的错误。
语言
Language
类是Jetcod IP Intelligence库的一部分,旨在根据CLDR(通用地域数据存储库)数据提供关于特定国家使用的语言的信息。此类允许您根据给定的国家代码检索有关语言、官方语言和与该国家代码关联的地域的详细信息。
以下是使用示例
use Jetcod\IpIntelligence\Models\Language; use Jetcod\IpIntelligence\Exceptions\LanguageNotFoundException; use Symfony\Component\Dotenv\Dotenv; // Create a Language instance for the United States ('US') $language = new Language('US'); try { // Retrieve all languages spoken in the United States $allLanguages = $language->all(); // Retrieve official and de facto official languages $officialLanguages = $language->officials(); // Retrieve the locale for the first official language $locale = $language->locale(); // Print the retrieved data print_r($allLanguages); print_r($officialLanguages); echo "Locale: $locale"; } catch (LanguageNotFoundException $e) { echo $e->getMessage(); }
此用法示例演示了如何创建Language实例、切换国家并检索该国家的语言信息。它还处理过程中可能发生的异常。
贡献
如果您想为此库做出贡献,请将其仓库分叉并提交拉取请求。我们欢迎错误修复、功能请求和其他贡献。
许可证
此库在MIT许可证下发布。请参阅LICENSE文件以获取更多信息。