<?php
namespace IPHub;
class Lookup {
public static function isBadIP(string $ip, string $key, bool $strict = false) {
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => "http://v2.api.iphub.info/ip/{$ip}",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ["X-Key: {$key}"]
]);
try {
$block = json_decode(curl_exec($ch))->block;
} catch (Exception $e) {
throw $e;
}
if ($block) {
if ($strict) {
return true;
} elseif (!$strict && $block === 1) {
return true;
}
}
return false;
}
}
To query an IP; you save the above php script to a file named iphub.class.php, require
it in your application then access the isBadIP method using the Scope Resolution Operator in the IPHub namespace:
<?php
require_once('iphub.class.php');
use IPHub\Lookup as IPHub;
$ip = $_SERVER['REMOTE_ADDR'];
// (string) $ip, (string) $key, (boolean) $strict
if (IPHub::isBadIP($ip, "YOUR_API_KEY_HERE")) {
die("Request blocked as you appear to be browsing from a VPN/Proxy/Server. Please contact IPHub.info if you believe this is a mistake. Your IP address is $ip.");
}
// Remainder of your page content here
Please note that the code above doesn’t handle exceptions. Use a try/catch statement to avoid throwing errors on your web app:
try {
$block = IPHub::isBadIP($ip, "YOUR_API_KEY_HERE");
} catch (Exception $e) {
$block = false;
}