Overview

Namespaces

  • whoisServerList

Classes

  • whoisServerList\WhoisApi

Exceptions

  • whoisServerList\RecoverableWhoisApiException
  • whoisServerList\WhoisApiException
  • Overview
  • Namespace
  • Class
  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:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 
<?php

namespace whoisServerList;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Promise;
use Psr\Http\Message\ResponseInterface;

/**
 * A Whois API.
 *
 * This is a client library for the service of http://whois-api.domaininformation.de/.
 * Register there to get an API key.
 *
 * With this API you can check if a domain name is available, get its
 * whois data or query an arbitrary whois server. The service is using
 * the whois list from https://github.com/whois-server-list/whois-server-list.
 * Also it avoids hitting any rate limits on the whois servers.
 *
 * Example:
 * <code>
 * use whoisServerList\WhoisApi;
 *
 * $whoisApi = new WhoisApi("apiKey");
 * echo $whoisApi->isAvailable("example.net") ? "available" : "registered";
 * </code>
 *
 * @author Markus Malkusch <markus@malkusch.de>
 * @link http://whois-api.domaininformation.de/ Whois API
 * @license http://www.wtfpl.net/txt/copying/ WTFPL
 */
class WhoisApi
{

    /**
     * @var ClientInterface HTTP client
     */
    private $client;

    /**
     * Builds a Domain API.
     *
     * Register at http://whois-api.domaininformation.de/ to get an API key.
     *
     * @param string $apiKey API key
     * @param string $endpoint optional endpoint, default is "https://whois-v0.p.mashape.com/".
     */
    public function __construct($apiKey, $endpoint = "https://whois-v0.p.mashape.com/")
    {
        if (empty($apiKey)) {
            throw new \InvalidArgumentException("API key is empty.");
        }
        if (empty($endpoint)) {
            throw new \InvalidArgumentException("Endpoint is empty.");
        }

        $this->client = new Client([
            "base_uri" => $endpoint,
            "headers" => ["X-Mashape-Key" => $apiKey],
            "http_errors" => false,
        ]);
    }

    /**
     * Checks if a domain is available.
     *
     * If a domain is available (i.e. not registered) this method
     * will return true.
     *
     * @param string $domain domain name, e.g. "example.net"
     * @return bool true if the domain is available, false otherwise.
     *
     * @throws RecoverableWhoisApiException API failed, but you can try again.
     *      This can happen if the upstream whois server did not respond in time.
     * @throws WhoisApiException API failed
     */
    public function isAvailable($domain)
    {
        if (empty($domain)) {
            throw new \InvalidArgumentException("The domain is empty.");
        }
        
        $body = $this->sendRequest("check", ["domain" => $domain]);
        $result = json_decode($body);
        return $result->available;
    }

    /**
     * Checks multiple domains if they are available.
     *
     * areAvailable(["example.net", "example.org", "example.com"])
     * could return this map:
     * <code>
     * [
     *   "example.net" => true,  // example.net is available
     *   "example.org" => false, // example.org is registered
     *   "example.com" => null   // example.com failed
     * ]
     * </code>
     *
     * @param string[] $domains domain names, e.g. ["example.net", "example.org"]
     * @return boolean[] map with the result for each domain. True means
     *      the domain is available, false not. NULL however means that the
     *      query failed for that domain.
     */
    public function areAvailable(array $domains)
    {
        $promises = [];
        foreach ($domains as $domain) {
            $promises[$domain] = $this->client->requestAsync(
                "GET",
                "check",
                ["query" => ["domain" => $domain]]
            );
        }
        $results = Promise\unwrap($promises);

        return array_map(function (ResponseInterface $response) {
            if ($response->getStatusCode() != 200) {
                return null;
            }
            $result = json_decode($response->getBody());
            return $result->available;

        }, $results);
    }
    
    /**
     * Returns the whois data for a domain.
     *
     * @param string $domain domain name, e.g. "example.net"
     * @return string response of the respective whois server
     *
     * @throws RecoverableWhoisApiException API failed, but you can try again.
     *      This can happen if the upstream whois server did not respond in time.
     * @throws WhoisApiException API failed
     */
    public function whois($domain)
    {
        if (empty($domain)) {
            throw new \InvalidArgumentException("The domain is empty.");
        }
        
        $body = $this->sendRequest("check", ["domain" => $domain]);
        $result = json_decode($body);
        return $result->whoisResponse;
    }
    
    /**
     * Queries a whois server.
     *
     * @param string $host hostname of the whois server, e.g. "whois.verisign-grs.com"
     * @param string $query query, e.g. "example.net"
     *
     * @return string response from the whois server
     *
     * @throws RecoverableWhoisApiException API failed, but you can try again.
     *      This can happen if the upstream whois server did not respond in time.
     * @throws WhoisApiException API failed
     */
    public function query($host, $query)
    {
        if (empty($host)) {
            throw new \InvalidArgumentException("The host is empty.");
        }
        if (empty($query)) {
            throw new \InvalidArgumentException("The query is empty.");
        }
        
        return $this->sendRequest("whois", ["host" => $host, "query" => $query]);
    }

    /**
     * Returns a list of all top and second level domains, which are
     * known to the Whois API.
     *
     * @return string[] all available top and second level domains.
     */
    public function domains()
    {
        $body = $this->sendRequest("domains");
        $result = json_decode($body);
        return $result;
    }
    
    /**
     * Sends a request and return the reponse body.
     *
     * @param string $path request path
     * @param string[] $parameters request paramters
     *
     * @return string response body
     *
     * @throws RecoverableWhoisApiException API failed, but you can try again.
     * @throws WhoisApiException API failed
     */
    private function sendRequest($path, array $parameters = [])
    {
        try {
            $response = $this->client->request("GET", $path, ["query" => $parameters]);

        } catch (\Exception $e) {
            throw new WhoisApiException("Transport failed.", 0, $e);
        }
            
        switch ($response->getStatusCode()) {

            case 200:
                return $response->getBody()->__toString();

            case 502:
            case 504:
                throw new RecoverableWhoisApiException(
                    "Try again: {$response->getBody()}",
                    $response->getStatusCode()
                );

            default:
                throw new WhoisApiException("API failed: {$response->getBody()}", $response->getStatusCode());
        }
    }
}
API documentation generated by ApiGen