check('49.207.48.1', ['IN']); * if (!$r['country_match'] || $r['is_vpn']) { ... } */ namespace IPzentra; class ApiException extends \RuntimeException { /** @var int HTTP status (0 for a network error) */ public $status; /** @var string Machine-readable code, for example rate_limited or invalid_key */ public $errorCode; /** @var array|null Decoded error body */ public $body; public function __construct(int $status, string $errorCode, string $message, ?array $body = null) { parent::__construct($message, $status); $this->status = $status; $this->errorCode = $errorCode; $this->body = $body; } } class Client { const VERSION = '1.0.0'; /** @var int|null Lookups left today, from the last response */ public $remainingToday = null; private $key; private $base; private $timeout; private $retries; /** Options: base_url, timeout (seconds, default 10), retries (on network errors and 5xx, default 1). */ public function __construct(string $apiKey, array $options = []) { $this->key = $apiKey; $this->base = rtrim($options['base_url'] ?? 'https://api.ipzentra.com', '/'); $this->timeout = (float) ($options['timeout'] ?? 10); $this->retries = (int) ($options['retries'] ?? 1); } /** Look up one IPv4 or IPv6 address. */ public function lookup(string $ip): array { return $this->request('GET', '/v1/' . rawurlencode($ip)); } /** Look up the IP address of the machine making the call. */ public function me(): array { return $this->request('GET', '/v1/'); } /** Look up an IP and compare it with the countries you expect. $countries: ['IN', 'SG'] or 'IN,SG'. */ public function check(string $ip, $countries): array { return $this->request('GET', '/v1/check?' . http_build_query(['ip' => $ip, 'country' => self::countries($countries)])); } /** Look up up to 100 IPs in one request. Returns ['count' => n, 'ok' => n, 'results' => [...]]. */ public function batch(array $ips, $countries = null): array { $body = ['ips' => array_values($ips)]; if ($countries !== null && $countries !== '' && $countries !== []) $body['country'] = self::countries($countries); return $this->request('POST', '/v1/batch', $body); } /** Service status. Does not use your key or quota. */ public function health(): array { return $this->request('GET', '/health'); } private static function countries($c): string { return strtoupper(str_replace(' ', '', is_array($c) ? implode(',', $c) : (string) $c)); } private function request(string $method, string $path, ?array $body = null): array { $attempt = 0; while (true) { $headers = []; $ch = curl_init($this->base . $path); $h = ['X-Api-Key: ' . $this->key, 'Accept: application/json', 'User-Agent: ipzentra-php/' . self::VERSION]; if ($body !== null) { $h[] = 'Content-Type: application/json'; curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body)); } curl_setopt_array($ch, [CURLOPT_CUSTOMREQUEST => $method, CURLOPT_HTTPHEADER => $h, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT_MS => (int) ($this->timeout * 1000), CURLOPT_CONNECTTIMEOUT_MS => 5000, CURLOPT_HEADERFUNCTION => function ($c, $line) use (&$headers) { $p = explode(':', $line, 2); if (count($p) === 2) $headers[strtolower(trim($p[0]))] = trim($p[1]); return strlen($line); }]); $raw = curl_exec($ch); $status = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE); $err = curl_error($ch); curl_close($ch); if (isset($headers['x-ratelimit-remaining-day'])) $this->remainingToday = (int) $headers['x-ratelimit-remaining-day']; if (($raw === false || $status >= 500) && $attempt < $this->retries) { $attempt++; usleep(300000 * $attempt); continue; } if ($raw === false) throw new ApiException(0, 'network_error', 'Could not reach IPzentra: ' . $err); $data = json_decode((string) $raw, true); if ($status >= 200 && $status < 300 && is_array($data)) return $data; $data = is_array($data) ? $data : null; throw new ApiException($status, (string) ($data['error'] ?? 'http_' . $status), (string) ($data['message'] ?? 'IPzentra returned HTTP ' . $status), $data); } } }