72 lines
2.9 KiB
PHP
72 lines
2.9 KiB
PHP
<?php
|
|
// Live test against the API (uses the hourly limit): php tests/live.php [domain ...]
|
|
// Without a key it uses the limit per IP. With the API key environment variable set, the account limit.
|
|
// WHOIS_TEST_API_URL points the test to another address of the API, such as a test server.
|
|
|
|
require __DIR__ . '/../autoload.php';
|
|
|
|
use InterwebMz\Whois\Client;
|
|
use InterwebMz\Whois\HttpTransport;
|
|
use InterwebMz\Whois\Exception\WhoisException;
|
|
|
|
$base = getenv('WHOIS_TEST_API_URL');
|
|
$http = new HttpTransport();
|
|
$transport = static function (string $method, string $url, array $headers, int $timeout) use ($http, $base) {
|
|
if ($base) {
|
|
$url = rtrim($base, '/') . substr($url, strlen(rtrim(Client::API_URL, '/')));
|
|
}
|
|
return $http->send($method, $url, $headers, $timeout);
|
|
};
|
|
|
|
$domains = array_slice($argv, 1) ?: ['example.co.mz', 'example.ao', 'example.com'];
|
|
$failures = 0;
|
|
|
|
foreach ([false, true] as $withKey) {
|
|
$options = ['transport' => $transport];
|
|
if (!$withKey) {
|
|
$options['api_key'] = '';
|
|
putenv(Client::KEY_ENV);
|
|
} elseif (!getenv(Client::KEY_ENV) && !getenv('WHOIS_TEST_KEY')) {
|
|
echo "\n(no key in the environment: skipping the lookups with a key)\n";
|
|
break;
|
|
} else {
|
|
$options['api_key'] = getenv('WHOIS_TEST_KEY') ?: getenv(Client::KEY_ENV);
|
|
}
|
|
$client = new Client($options);
|
|
echo "\n== " . ($client->hasApiKey() ? 'with a key' : 'without a key') . "\n";
|
|
foreach ($domains as $domain) {
|
|
try {
|
|
$r = $client->lookup($domain);
|
|
printf("%-28s %-11s expires %-10s price %s %s, %d alternatives\n", $r->domain(), $r->status(),
|
|
$r->expiresAt() ? $r->expiresAt()->format('Y-m-d') : '-', $r->registerPrice() ?? '-', $r->currency() ?? '',
|
|
count($r->alternatives()));
|
|
if ($r->domain() === '' || !in_array($r->status(), ['available', 'registered', 'unknown', 'not_allowed'], true)) {
|
|
$failures++;
|
|
}
|
|
} catch (WhoisException $e) {
|
|
printf("%-28s ERROR %s (%s, HTTP %d)\n", $domain, $e->getMessage(), $e->getErrorCode(), $e->getHttpStatus());
|
|
$failures++;
|
|
}
|
|
}
|
|
$limit = $client->lastRateLimit();
|
|
echo 'Rate limit: ' . ($limit ? json_encode($limit) : 'not sent') . "\n";
|
|
|
|
$many = $client->lookupMany($domains);
|
|
echo 'Several in one request: ' . implode(', ', array_map(static function ($r) {
|
|
return $r->domain() . '=' . $r->status();
|
|
}, $many)) . "\n";
|
|
if (count($many) !== count(array_unique($domains))) {
|
|
$failures++;
|
|
}
|
|
|
|
try {
|
|
$client->lookup('example.invalidtld');
|
|
echo "Unsupported extension: no error (unexpected)\n";
|
|
$failures++;
|
|
} catch (WhoisException $e) {
|
|
echo 'Unsupported extension: ' . get_class($e) . ' ' . $e->getErrorCode() . ' ' . ($e->getReason() ?? '') . "\n";
|
|
}
|
|
}
|
|
|
|
echo $failures ? "\n$failures problem(s).\n" : "\nAll live lookups answered.\n";
|
|
exit($failures ? 1 : 0);
|