Geoip2 Update is a php tool for updating Maxmind GeoLite2 and GeoIP2 databases from your script, application or via Composer.
The minimum requirement of this library is for your web server to support PHP >= 5.3.0 with curl
extension and an optional zip
extension if you want to update the csv
databases.
If you do not have Composer, you may install it by following the instructions at getcomposer.org.
You can then install this library using the following command:
composer require tronovav/geoip2-update
To update Geoip2 databases via Composer, you can set up an update call in your composer.json
.
Each time the composer update
command is invoked, the library will check for updates on the "maxmind.com" server and update the Geoip2 databases if necessary.
You can also update only GeoIP2
databases without updating all project dependencies:
composer update tronovav/geoip2-update
.
1.1. Use in-line configuration options:
# composer.json
"scripts": {
"post-update-cmd": [
"tronovav\\GeoIP2Update\\ComposerClient::run"
]
},
"extra": {
"tronovav\\GeoIP2Update\\ComposerClient::run": {
"license_key": "MAXMIND_LICENSE_KEY",
"dir": "DESTINATION_DIRECTORY_PATH",
"editions": ["GeoLite2-ASN", "GeoLite2-City", "GeoLite2-Country"]
}
}
Parameters in the scripts
section:
Just add the "post-update-cmd": "tronovav\\GeoIP2Update\\ComposerClient::run"
line to update databases via Composer.
Parameters in the extra
section:
license_key
(required) - You can see your license key information on your account License Keys page at maxmind.com.dir
(required) - Absolute path to theGeoip2
local database storage directory. Or you can alias part of the path relative to your composer.json. For example@composer/path-to-db-storage
. The script itself will find the absolute path to thecomposer.json
file in your project and thepath-to-db-storage
directory for storingGeoip2
databases relative tocomposer.json
. You can also specify@composer/../path-to-db-storage
. The main thing is that you yourself understand which path to storing the database you specify.editions
- List of database editions that you want to update. Maxmind.com offers databases for free download:GeoLite2-ASN
,GeoLite2-City
,GeoLite2-Country
,GeoLite2-ASN-CSV
,GeoLite2-City-CSV
,GeoLite2-Country-CSV
. If you do not specify theeditions
parameter, then the databases will be updated:GeoLite2-ASN
,GeoLite2-City
,GeoLite2-Country
. Otherwise, only the editions that you specified will be updated. See available editions in your maxmind.com account.
1.2. Use GeoIP.conf
file:
Instead of the parameters license_key
and editions
, you can specify the path to the configuration file.
The configuration file format fully complies with the recommendations of maxmind.com on the documentation page:
Obtain GeoIP.conf with Account Information
# composer.json
"scripts": {
"post-update-cmd": [
"tronovav\\GeoIP2Update\\ComposerClient::run"
]
},
"extra": {
"tronovav\\GeoIP2Update\\ComposerClient::run": {
"dir": "DESTINATION_DIRECTORY_PATH",
"geoipConfFile": "DESTINATION_GEOIP_CONFIG_FILE"
}
}
Parameters in the extra
section:
dir
(required) - Absolute path to theGeoip2
local database storage directory. Or you can alias part of the path relative to your composer.json. For example@composer/path-to-db-storage
. The script itself will find the absolute path to thecomposer.json
file in your project and thepath-to-db-storage
directory for storingGeoip2
databases relative tocomposer.json
. You can also specify@composer/../path-to-db-storage
or another path relative to your filecomposer.json
.geoipConfFile
(required) - The absolute path to the configuration file. Or you can alias part of the path relative to your composer.json. For example@composer/../GeoIP.conf
.
You can use this option to update GeoIP2
databases from your project, or use Cron
on Linux, or use Task Scheduler
on Windows.
2.1. Use in-line configuration options:
<?php
require 'vendor/autoload.php';
// configuration
$client = new \tronovav\GeoIP2Update\Client(array(
'license_key' => 'MAXMIND_LICENSE_KEY',
'dir' => 'DESTINATION_DIRECTORY_PATH',
'editions' => array('GeoLite2-ASN', 'GeoLite2-City', 'GeoLite2-Country'),
));
// run update
$client->run();
Params:
license_key
(required) - You can see your license key information on your account License Keys page at maxmind.com.dir
(required) - Absolute path to the local storage ofGeoip2
databases.editions
- List of database editions that you want to update. Maxmind.com offers databases for free download:GeoLite2-ASN
,GeoLite2-City
,GeoLite2-Country
,GeoLite2-ASN-CSV
,GeoLite2-City-CSV
,GeoLite2-Country-CSV
. If you do not specify theeditions
parameter, then the databases will be updated:GeoLite2-ASN
,GeoLite2-City
,GeoLite2-Country
. Otherwise, only the editions that you specified will be updated. See available editions in your maxmind.com account.
2.2. Use GeoIP.conf
file:
Instead of the parameters license_key
and editions
, you can specify the path to the configuration file.
The configuration file format fully complies with the recommendations of maxmind.com on the documentation page:
Obtain GeoIP.conf with Account Information
<?php
require 'vendor/autoload.php';
// configuration
$client = new \tronovav\GeoIP2Update\Client(array(
'dir' => 'DESTINATION_DIRECTORY_PATH',
"geoipConfFile" => "DESTINATION_GEOIP_CONFIG_FILE",
));
// run update
$client->run();
Params:
dir
(required) - Absolute path to the local storage ofGeoip2
databases.geoipConfFile
(required) - The absolute path to the configuration file.
After the update, you can get information about the result, if you need it:
print_r($client->updated()); // update result
print_r($client->errors()); // update errors
Available Edition ID
databases that you can specify in the editions
parameter or in the configuration file to update them.
-
GeoLite2-ASN
-
GeoLite2-City
-
GeoLite2-Country
-
GeoLite2-ASN-CSV
-
GeoLite2-City-CSV
-
GeoLite2-Country-CSV
-
GeoIP2-ASN
-
GeoIP2-City
-
GeoIP2-Country
-
GeoIP2-ASN-CSV
-
GeoIP2-City-CSV
-
GeoIP2-Country-CSV
See available Edition ID
databases for updates in your maxmind.com account.
The atomicity of the update operation is implemented at the level of the database files.
The structure of the mmdb and csv databases is different. Mmdb databases consist of a single database file. Thus, when updating the mmdb databases, the operation is completely atomic and errors associated with the short-term absence of the mmdb file during the database update are excluded. Since the files are atomically replaced with new ones.
CSV databases consist of multiple files. When upgrading versions of the CSV database, each CSV file is also replaced atomically, and there is no chance of a file missing during the upgrade.
This software is Copyright (c) 2021 by Andrey Tronov.
This is free software, licensed under the MIT License.