Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

ExampleGeolite2Country.EN

Ivan Dudarev edited this page Oct 9, 2017 · 5 revisions

Documentation / Examples / Example DB from GeoLite2 Country

language: EN RU

<?php
$tmpDir = __DIR__.DIRECTORY_SEPARATOR.'tmp';
$converter = new \Ddrv\Iptool\Converter($tmpDir);
$dbFile = __DIR__.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'iptool.geo.country.dat';

$url = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country-CSV.zip';
$tmpFile = $tmpDir . DIRECTORY_SEPARATOR . 'geolite2country.zip';
copy($url,$tmpFile);
$zip = new ZipArchive();
if ($zip->open($tmpFile) !== true) die;
$i = -1;
$zipPath = null;
do {
    $i++;
    $csv = $zip->getNameIndex($i);
    preg_match('/(?<file>(?<zipPath>.*)\/GeoLite2\-Country\-Blocks\-IPv4\.csv)$/ui', $csv, $m);
} while ($i < $zip->numFiles && empty($m['file']));
$zipPath = $m['zipPath'];
$zip->close();

$locations = 'zip://' . $tmpFile . '#'.$zipPath.DIRECTORY_SEPARATOR.'GeoLite2-Country-Locations-en.csv';
$networks = 'zip://' . $tmpFile . '#' . $m['file'];

/**
 * Set author.
 */
$converter->setAuthor('Ivan Dudarev');

/**
 * Set license.
 */
$converter->setLicense('MIT');

/**
 * Add source files.
 */
$converter->addCSV('locations',$locations,1);
$converter->addCSV('networks',$networks,1);

/**
 * Add register Country.
 **/
$country = array(
    'code' => array(
        'type' => 'string',
        'column' => 4,
        'transform' => 'low',
    ),
    'name' => array(
        'type' => 'string',
        'column' => 5,
    ),
);
$converter->addRegister('country','locations',0, $country);

/**
 * Add networks.
 */
$data = array(
     'country' => 1,
);
$converter->addNetworks('networks', 'inetnum', 0, 0, $data);

/**
 * Create Database.
 */
$converter->create($dbFile);

/**
 * Delete temporary file
 */
unlink($tmpFile);

/**
 * Get information about created database
 */
$iptool = new \Ddrv\Iptool\Iptool($dbFile);
print_r($iptool->about());

/**
 * Search IP Address data
 */
print_r($iptool->find('95.215.84.0'));
Clone this wiki locally