webiny / geoip
dev-master
2016-09-11 20:36 UTC
Requires
- php: >=5.5.9
- webiny/entity: ^1.3 || dev-master
- webiny/http: ^1.3 || dev-master
Requires (Dev)
- mybuilder/phpunit-accelerator: dev-master
- phpunit/phpunit: ~4
This package is not auto-updated.
Last update: 2024-09-14 18:52:51 UTC
README
GeoIp是一个简单的组件,它为给定的IPv4或IPv6地址提供位置信息。
示例
$geo = new \Webiny\GeoIp\GeoIp(new \Webiny\GeoIp\Provider\FreeGeoIp\FreeGeoIp()); $location = $geo->getGeoIpLocation('8.8.8.8'); // output array( 'continentCode' => NULL, 'continentName' => NULL, 'countryCode' => 'US', 'countryName' => 'United States', 'cityName' => 'Mountain View', 'subdivision1Code' => 'CA', 'subdivision1Name' => 'California', 'subdivision2Code' => NULL, 'subdivision2Name' => NULL, 'timeZone' => 'America/Los_Angeles', )
根据提供商不同,可用的信息也会不同。GeoIp标准库包含2个提供商
FreeGeoIp
:使用https://freegeoip.net/ APIMaxMindGeoLite2
:使用MaxMind提供的GeoLite2数据库(需要Mongo数据库)
安装GeoIp
安装组件的最佳方式是使用Composer。
composer require webiny/geoip
要查看包的附加版本,请访问Packagist页面。
设置
FreeGeoIp
如果您打算使用FreeGeoIp
API,无需设置,只需创建一个提供商实例并将其传递给GeoIp
类,如下所示
$providerInstance = new \Webiny\GeoIp\Provider\FreeGeoIp\FreeGeoIp() $geo = new \Webiny\GeoIp\GeoIp($providerInstance); $location = $geo->getGeoIpLocation('8.8.8.8');
MaxMindGeoLite2
此提供商需要一个Mongo数据库,它使用Webiny\Mongo
组件进行配置。除了数据库外,还需要其他一些参数,以下是一个示例配置
MaxMind: GeoLite2Url: http://geolite.maxmind.com/download/geoip/database/GeoLite2-City-CSV.zip Language: en Entity: Database: GeoIp Mongo: Services: GeoIp: Class: \Webiny\Component\Mongo\Mongo Arguments: Host: 127.0.0.1:27017 Database: YourDatabase ## CHANGE THIS Username: null Password: null CollectionPrefix: ''
MaxMind
部分定义了GeoLite2数据库的下载位置和从数据库导入的位置名称的语言。`Entity`和`Mongo`部分是标准配置,如`Webiny/Entity`和`Webiny/Mongo`组件下所述。
一旦您设置了配置,请在终端运行以下命令
php /path/to/src/Webiny/GeoIp/Provider/MaxMindGeoLite2/run-installer.php abs-path-to-your-config-yaml-file
这将为您下载GeoLite2数据库并设置Mongo集合。请注意,此过程可能需要15-30分钟,具体取决于您的硬件。
安装完成后,您可以使用提供商如下所示
$config = 'abs-path-to-your-config-yaml-file'; $provider = new Webiny\GeoIp\Provider\MaxMindGeoLite2\MaxMindGeoLite2($config); $geo = new \Webiny\GeoIp\GeoIp($provider); $location = $geo->getGeoIpLocation('8.8.8.8');
自定义提供商
如果您想实现自定义提供商,只需为您的提供商创建一个实现Webiny\GeoIp\ProviderInterface
的类;
位置类
位置类是地理IP查询的结果。根据提供商,信息的数量可能有所不同。总体而言,典型的查询结果如下所示
Webiny/GeoIp/Location::__set_state(array( 'continentCode' => 'NA', 'continentName' => 'North America', 'countryCode' => 'US', 'countryName' => 'United States', 'cityName' => 'Mountain View', 'subdivision1Code' => 'CA', 'subdivision1Name' => 'California', 'subdivision2Code' => '', 'subdivision2Name' => '', 'timeZone' => 'America/Los_Angeles', ))
请注意,如果无法确定位置,则将返回false
而不是Location
实例。
许可和贡献
贡献 > 随意发送PR。
许可 > MIT
资源
要运行单元测试,您需要使用以下命令
$ cd path/to/GeoIp/
$ composer install
$ phpunit