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

Commit ff711ca

Browse files
committed
new example. Create database from Maxmind GeoLite2 City
1 parent 29983ec commit ff711ca

File tree

5 files changed

+111
-8
lines changed

5 files changed

+111
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ php composer.phar install
1818
# Create your IP addresses database
1919
See examples for create database:
2020
* Geo from Maxmind GeoLite2 Country `examples/geoFromMaxmindCountry.php`
21+
* Geo from Maxmind GeoLite2 City `examples/geoFromMaxmindCity.php`
2122

2223
# Use
2324
```php

examples/data/iptool.geo.city.dat

23.9 MB
Binary file not shown.
0 Bytes
Binary file not shown.

examples/geoFromMaxmindCity.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
ini_set("memory_limit", "64M");
3+
require_once(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'Converter.php');
4+
require_once(__DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'Iptool.php');
5+
$tmpDir = __DIR__.DIRECTORY_SEPARATOR.'tmp';
6+
$converter = new \Ddrv\Iptool\Converter($tmpDir);
7+
$dbFile = __DIR__.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'iptool.geo.city.dat';
8+
9+
$url = 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCity_CSV/GeoLiteCity-latest.zip';
10+
$tmpFile = $tmpDir . DIRECTORY_SEPARATOR . 'geolite2city.zip';
11+
copy($url,$tmpFile);
12+
$zip = new ZipArchive();
13+
if ($zip->open($tmpFile) !== true) die;
14+
$i = -1;
15+
$zipPath = null;
16+
do {
17+
$i++;
18+
$csv = $zip->getNameIndex($i);
19+
preg_match('/(?<file>(?<zipPath>.*)\/GeoLiteCity\-Blocks\.csv)$/ui', $csv, $m);
20+
} while ($i < $zip->numFiles && empty($m['file']));
21+
$zipPath = $m['zipPath'];
22+
$zip->close();
23+
24+
$locations = 'zip://' . $tmpFile . '#'.$zipPath.DIRECTORY_SEPARATOR.'GeoLiteCity-Location.csv';
25+
$networks = 'zip://' . $tmpFile . '#' . $m['file'];
26+
27+
/**
28+
* Set author.
29+
*/
30+
$converter->setAuthor('Ivan Dudarev');
31+
32+
/**
33+
* Set license.
34+
*/
35+
$converter->setLicense('MIT');
36+
37+
/**
38+
* Add source files.
39+
*/
40+
$converter->addCSV('locations',$locations,2);
41+
$converter->addCSV('networks',$networks,2);
42+
43+
/**
44+
* Add register Geo.
45+
**/
46+
$geo = array(
47+
'geonames' => array(
48+
'type' => 'int',
49+
'column' => 0,
50+
),
51+
'country' => array(
52+
'type' => 'string',
53+
'column' => 1,
54+
'transform' => 'low',
55+
),
56+
'region' => array(
57+
'type' => 'string',
58+
'column' => 2,
59+
),
60+
'city' => array(
61+
'type' => 'string',
62+
'column' => 3,
63+
),
64+
'latitude' => array(
65+
'type' => 'double',
66+
'column' => 5,
67+
),
68+
'longitude' => array(
69+
'type' => 'double',
70+
'column' => 6,
71+
),
72+
);
73+
$converter->addRegister('geo','locations',0, $geo);
74+
75+
/**
76+
* Add networks.
77+
*/
78+
$data = array(
79+
'geo' => 2,
80+
);
81+
$converter->addNetworks('networks', 'long', 0, 1, $data);
82+
83+
/**
84+
* Create Database.
85+
*/
86+
$converter->create($dbFile);
87+
88+
/**
89+
* Delete temporary file
90+
*/
91+
unlink($tmpFile);
92+
93+
/**
94+
* Get information about created database
95+
*/
96+
$iptool = new \Ddrv\Iptool\Iptool($dbFile);
97+
print_r($iptool->about());
98+
99+
/**
100+
* Search IP Address data
101+
*/
102+
print_r($iptool->find('95.215.84.0'));

examples/geoFromMaxmindCountry.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
$dbFile = __DIR__.DIRECTORY_SEPARATOR.'data'.DIRECTORY_SEPARATOR.'iptool.geo.country.dat';
88

99
$url = 'http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country-CSV.zip';
10-
$tmpFile = $tmpDir . DIRECTORY_SEPARATOR . 'tmp.zip';
10+
$tmpFile = $tmpDir . DIRECTORY_SEPARATOR . 'geolite2country.zip';
1111
copy($url,$tmpFile);
1212
$zip = new ZipArchive();
1313
if ($zip->open($tmpFile) !== true) die;
@@ -16,13 +16,13 @@
1616
do {
1717
$i++;
1818
$csv = $zip->getNameIndex($i);
19-
preg_match('/(?<file>(?<zipPath>.*)\/GeoLite2-Country-Blocks-IPv4\.csv)$/ui', $csv, $m);
19+
preg_match('/(?<file>(?<zipPath>.*)\/GeoLite2\-Country\-Blocks\-IPv4\.csv)$/ui', $csv, $m);
2020
} while ($i < $zip->numFiles && empty($m['file']));
2121
$zipPath = $m['zipPath'];
2222
$zip->close();
2323

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

2727
/**
2828
* Set author.
@@ -37,8 +37,8 @@
3737
/**
3838
* Add source files.
3939
*/
40-
$converter->addCSV('locations',$locations);
41-
$converter->addCSV('geo',$geo);
40+
$converter->addCSV('locations',$locations,1);
41+
$converter->addCSV('networks',$networks,1);
4242

4343
/**
4444
* Add register Country.
@@ -59,10 +59,10 @@
5959
/**
6060
* Add networks.
6161
*/
62-
$geo = array(
62+
$data = array(
6363
'country' => 1,
6464
);
65-
$converter->addNetworks('geo', 'inetnum', 0, 0, $geo);
65+
$converter->addNetworks('networks', 'inetnum', 0, 0, $data);
6666

6767
/**
6868
* Create Database.
@@ -83,4 +83,4 @@
8383
/**
8484
* Search IP Address data
8585
*/
86-
print_r($iptool->find('81.32.17.89'));
86+
print_r($iptool->find('95.215.84.0'));

0 commit comments

Comments
 (0)