-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathipinfo
More file actions
62 lines (54 loc) · 1.8 KB
/
ipinfo
File metadata and controls
62 lines (54 loc) · 1.8 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env php
<?php
function curl_get_contents($url, $json = false, $show_error = false) {
if (!function_exists('curl_init')) { return file_get_contents($url); } // fallback
$ch = curl_init();
$options = array(
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
if ($response === false && $show_error) {
echo 'ERROR: ' . curl_error($ch)."\n";
}
curl_close($ch);
if ($json) {
return json_decode($response, true);
}
return $response;
}
// conf
define('API_URL', 'http://ipinfo.io/');
define('DEBUG', false);
if (!DEBUG) { error_reporting(0); }
// throw errors on wrong usage..
if (php_sapi_name() !== 'cli') exit('ERROR: This script must be executed in cli.');
$addr = trim($argv[1]);
if (!empty($addr)) {
if (preg_match('/[A-z0-9\.]+\.[A-z]+/', $addr)) { // is a domain?
$addr = gethostbyname($addr);
if ($addr === trim($argv[1])) {
exit("ERROR: Domain not resolved.".PHP_EOL);
}
} else if (!filter_var($addr, FILTER_VALIDATE_IP)) {
exit("ERROR: Argument was not a valid IP or domain.".PHP_EOL);
}
$url = API_URL.$addr.'/json';
} else {
$url = API_URL.'/json';
}
// get the data
$ip_data = curl_get_contents($url, true, DEBUG);
if ($ip_data === false) exit("ERROR: The API is not available.".PHP_EOL);
if (!empty($ip_data)) {
$maxKeyLength = max(array_map('strlen', array_keys($ip_data))) + 1;
foreach ($ip_data as $key => $value) {
if (empty($value)) continue;
echo str_pad(strtoupper($key), $maxKeyLength) . '= ' . $value . PHP_EOL;
}
} else {
exit("ERROR: API result is empty.".PHP_EOL);
}