confidencesapp/zend-geoip

MaxMind GeoIP Module for Zend Framework 3

1.0.2 2017-04-19 14:15 UTC

README

Build Status Latest Stable Version Coverage Status

Maxmind Geoip module for Zend Framework 3

Installation

This module is available on Github. Via composer.json

{
    "require": {
        "confidencesapp/zend-geoip": "1.*"
    }
}

and add Confidences\ZendGeoip module name to application.config.php

To download data file from http://dev.maxmind.com/geoip/legacy/geolite/ use Zend\Console (you can add this to crontab):

php public/index.php geoip download

Or use autoupdate database during install/update in composer (just add this lines to composer.json and run composer):

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

Add Confidences\ZendGeoip to the modules array in your application.config.php, preferably as the first module.

Console usage

You can download GeoIP database from application console:

php public/index.php geoip download

There are optional parameters:

  • --no-clobber Don't overwrite an existing db file,
  • -q Turn off output,

Usage

Default ZendGeoip returns Record object created by current user's IP address.

In controller:

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

By view helper:

Returns city name for current IP:

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

Returns country name for given IP:

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

You can also implements \Confidences\ZendGeoip\IpAwareInterface interface and then use instance in service/helper:

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

Available getter methods via \Confidences\ZendGeoip\Entity\Record:

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

Events

Module supports \Zend\EventManager.

Class Event name Description Params
Confidences\ZendGeoip\Controller\ConsoleController downloadAction.exists If no-clobber is enabled and file exists path (to dat file)
Confidences\ZendGeoip\Controller\ConsoleController downloadAction.pre Before unzip file path (to dat file), response (gziped response object)
Confidences\ZendGeoip\Controller\ConsoleController downloadAction.post After unzip file path (to dat file)
Confidences\ZendGeoip\Service\Geoip getIp After read IP ip (ip address)
Confidences\ZendGeoip\Service\Geoip getRecord After created record record (instance of Confidences\ZendGeoip\Entity\RecordInterface)
Confidences\ZendGeoip\Service\Geoip getRegions After first loading regions names regions