-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
38 lines (28 loc) · 775 Bytes
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
require 'vendor/autoload.php';
use GeoIp2\Database\Reader;
/*
This product includes GeoLite2 data created by MaxMind, available from
<a href="http://www.maxmind.com">http://www.maxmind.com</a>.
*/
header('Content-type: application/json');
header("Access-Control-Allow-Origin: *");
try {
$reader = new Reader('./GeoLite2-City.mmdb');
$options = array(
'options' => array(
'regexp' => '/(?:\d{1,3}\.){3}\d{1,3}/'
)
);
$ip = filter_var($_GET['ip'], FILTER_VALIDATE_REGEXP, $options);
if ($ip !== FALSE) {
$location = $reader->city($ip);
$location_json = json_encode($location->raw);
echo $location_json;
} else {
echo '{"message": "Bad IPv4 address given"}';
}
}
catch (Exception $e) {
echo $e->getMessage();
}