snapshotpl/zf-snap-geoip

Zend Framework 2 的 MaxMind GeoIP 模块

2.6.0 2019-07-07 09:37 UTC

README

Zend Framework 2 的 Maxmind Geoip 模块

由 Witold Wasiczko 创建

用法

默认的 ZfSnapGeoip 返回由当前用户 IP 地址创建的 Record 对象。

在控制器中

$record = $this->getServiceLocator()->get('geoip')->getRecord();
echo $record->getCity();
$record = $this->getServiceLocator()->get('geoip')->getRecord('216.239.51.99');
echo $record->getLongitude();
echo $record->getLatitude();

通过视图助手

返回当前 IP 的城市名称

<?php echo $this->geoip() ?>

返回给定 IP 的国家名称

<?php echo $this->geoip('184.106.35.179')->getCountryName() ?>

您还可以实现 \ZfSnapGeoip\IpAwareInterface 接口,然后在服务/助手中使用实例

<?php echo $this->geoip($user)->getTimezone() ?>

通过 \ZfSnapGeoip\Entity\Record 可用的获取器方法

getAreaCode()
getCity()
getContinentCode()
getCountryCode()
getCountryCode3()
getCountryName()
getDmaCode()
getLatitude()
getLongitude()
getMetroCode()
getPostalCode()
getRegion()
getRegionName()
getTimezone()

事件

模块支持 \Zend\EventManager

如何安装?

通过 composer.json

{
    "require": {
        "snapshotpl/zf-snap-geoip": "2.*"
    }
}

并将 ZfSnapGeoip 模块名称添加到 application.config.php

http://dev.maxmind.com/geoip/legacy/geolite/ 下载数据文件,使用 Zend\Console(您可以将此添加到 crontab)

php index/public.php geoip download

或在 composer 的安装/更新期间使用自动更新数据库(只需将这些行添加到 composer.json 并运行 composer)

{
    "scripts": {
        "post-install-cmd": [
            "ZfSnapGeoip\\Composer\\ScriptHandler::downloadData"
        ],
        "post-update-cmd": [
            "ZfSnapGeoip\\Composer\\ScriptHandler::downloadData"
        ]
    }
}

控制台用法

您可以从应用程序控制台下载 GeoIP 数据库

php public/index.php geoip download

有可选参数

  • --no-clobber 不要覆盖现有 db 文件
  • -q 关闭输出