Skip to content

Instantly share code, notes, and snippets.

@tomac4t
Last active March 12, 2023 10:32
Show Gist options
  • Save tomac4t/abc4712ceaa0fd9819d98d704c921bbe to your computer and use it in GitHub Desktop.
Save tomac4t/abc4712ceaa0fd9819d98d704c921bbe to your computer and use it in GitHub Desktop.
MaxMind DB Reader PHP API example code https://github.com/maxmind/MaxMind-DB-Reader-php
<?php
require_once 'MaxMind-DB-Reader-php/autoload.php';
use MaxMind\Db\Reader;
$ipAddress = $_SERVER['REMOTE_ADDR'];
if (isset ($_POST["ip"]) ) {
if (filter_var($_POST["ip"], FILTER_VALIDATE_IP)) {
$ipAddress = $_POST["ip"];
} else {
header("HTTP/2 400 Bad Request");
die();
}
} elseif (isset ($_GET["ip"]) ) {
if (filter_var($_GET["ip"], FILTER_VALIDATE_IP)) {
$ipAddress = $_GET["ip"];
} else {
header("HTTP/2 400 Bad Request");
die();
}
}
$databaseFile = "GeoLite2-City.mmdb";
$reader = new Reader($databaseFile);
//print_r($reader->get($ipAddress));
$geoinfo = $reader->get($ipAddress);
$reader->close();
$asnFile = "GeoLite2-ASN.mmdb";
$asnreader = new Reader($asnFile);
//print_r($asnreader->get($ipAddress));
$asninfo = $asnreader->get($ipAddress);
$asnreader->close();
$dbipFile = "DBIP-City-Lite.mmdb";
$dbipreader = new Reader($dbipFile);
//print_r($dbipreader->get($ipAddress));
$dbipinfo = $dbipreader->get($ipAddress);
$dbipreader->close();
header('Content-Type: application/json; charset=utf-8');
header('access-control-allow-origin: *');
$city_update=filemtime("GeoLite2-City.mmdb");
$dbip_update=filemtime("DBIP-City-Lite.mmdb");
?>
{
"ip": "<?php echo $ipAddress;?>",
"geolite2":{
"city": "<?php echo $geoinfo['city']['names']['en'] ?? '';?>",
"subdivisions": "<?php echo $geoinfo['subdivisions'][0]['names']['en'] ?? '';?>",
"country": "<?php echo $geoinfo['country']['names']['en'] ?? '';?>",
"continent": "<?php echo $geoinfo['continent']['names']['en'] ?? '';?>",
"subdivisions_code": "<?php echo $geoinfo['subdivisions'][0]['iso_code'] ?? '';?>",
"country_code": "<?php echo $geoinfo['country']['iso_code'] ?? '';?>",
"continent_code": "<?php echo $geoinfo['continent']['code'] ?? '';?>",
"accuracy_radius": <?php echo $geoinfo['location']['accuracy_radius'] ?? 'null';?>,
"latitude": "<?php echo $geoinfo['location']['latitude'] ?? '';?>",
"longitude": "<?php echo $geoinfo['location']['longitude'] ?? '';?>",
"timezone": "<?php echo $geoinfo['location']['time_zone'] ?? '';?>",
"asn": <?php echo $asninfo['autonomous_system_number'] ?? 'null';?>,
"organization": "<?php echo $asninfo['autonomous_system_organization'] ?? '';?>",
"last_updated": <?php echo $city_update;?>
},
"dbip":{
"city": "<?php echo $dbipinfo['city']['names']['en'] ?? '';?>",
"subdivisions": "<?php echo $dbipinfo['subdivisions'][0]['names']['en'] ?? '';?>",
"country": "<?php echo $dbipinfo['country']['names']['en'] ?? '';?>",
"continent": "<?php echo $dbipinfo['continent']['names']['en'] ?? '';?>",
"country_code": "<?php echo $dbipinfo['country']['iso_code'] ?? '';?>",
"continent_code": "<?php echo $dbipinfo['continent']['code'] ?? '';?>",
"latitude": "<?php echo $dbipinfo['location']['latitude'] ?? '';?>",
"longitude": "<?php echo $dbipinfo['location']['longitude'] ?? '';?>",
"last_updated": <?php echo $dbip_update;?>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment