The code sample below demonstrates how to use Online Domain Tools Whois API to perform a large number of WHOIS lookups of either domain or IP records. It uses asynchronous callback method in order to minimize needs for settings of the environment where the script can be successfully run. Any PHP 5.3+ hosting should be able to run this script. The script loads WHOIS queries from the database and processes them one by one. Please read the initial comment to get more information on how to use this script and how it works.
<?php // // This script is an example of how to use Online Domain Tools Whois API to perform // a large number of WHOIS requests. It allows you to perform any amount of WHOIS queries // written into an input file. // // This script is provided "AS IS" without warranties of any kind, either express or implied. // Everyone is allowed to use it, copy it, or create derived products from it, completely // free of charge, provided that Online Domain Tools TOS (see the link below) is respected // and not violated. // // // Copyright (c) 2015 - Online Domain Tools // // Relevant links: // * http://whois.online-domain-tools.com/ - ODT Whois // * http://online-domain-tools.com/information/api - ODT API specification // * http://online-domain-tools.com/information/privacy-policy-and-tos - ODT TOS // * http://online-domain-tools.com/ - Online Domain Tools homepage // // // How does it work? // * This script loads input queries stored in $whoisInputFile, separated by newlines. // * Each time it runes it removes the first line from the input file and sends it // as a query to ODT API server. // * Asynchronous callback mode is used with ODT API so that this script is called by ODT // API server when the job is done. // * ODT uses POST request with JSON data to report the results to the callback. // * The results from the server are written to $whoisResultsFile. Each result is written // on a separate line in the format "QUERY::RESULT" (without the quotes). // * The script logs its progress to $logFile, so in case of any problems, check the log // file. // // How to use it? // * You have to have ODT account with API enabled and obtain API key and secret. See PDF // ODT API specification and its section 1. // * Fill in your ODT API key and secret to $apiKey and $apiSecret global variables. // * Create a list of WHOIS queries and store them to whois-input.txt. // Separate queries in the input file with newlines. Each query should be either a domain // or IP address. // * Copy whois-input.txt and this script to the same folder on a PHP hosting server. // * Execute the script using your browser - simply access the script's URL. // * Wait until the input file is empty or until log file does not grow. // * If whois-input.txt is empty, the work is done. Otherwise, check whois-log.txt to see // the problem. // # Settings // Your API key & API secret $apiKey = 'Your API key here'; // ODT-API-XXX $apiSecret = 'Your API secret here'; // Is logging enabled $loggingEnabled = true; // Name of input file, in which queries waiting for processing are separated by newlines. $whoisInputFile = __DIR__ . '/whois-input.txt'; // Name of output file with unparsed results. $whoisResultsFile = __DIR__ . '/whois-results.txt'; // Name of log file. $logFile = __DIR__ . '/whois-log.txt'; // URL of this script. $thisUrl = "http" . (!empty($_SERVER['HTTPS'])?"s":"") . "://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; ini_set('display_errors', 1); error_reporting(E_ALL); // Setup unique token $token = uniqid(); if (isset($_GET["token"])) $token = $_GET["token"]; /** * Appends $message to file $fileName. * * @param string $fileName Name of the file to append to. * @param string $data Data to append. */ function fileAppend($fileName, $data) { $fp = fopen($fileName, "a"); flock($fp, LOCK_EX); fwrite($fp, $data); flock($fp, LOCK_UN); fclose($fp); } /** * Appends $message to log file with time stamp. * * @param string $message */ function mlog($message) { global $logFile, $loggingEnabled, $token; if ($loggingEnabled) { $dateTime = new \DateTime('now', new \DateTimeZone('UTC')); $time = $dateTime->format('Y-m-d H:i:s'); $line = '[' . $time . ']<' . $token . '> ' . $message . "\n"; fileAppend($logFile, $line); } } /** * Appends $message to output file. * * @param string $message */ function writeOutput($message) { global $whoisResultsFile; $line = $message . "\n"; fileAppend($whoisResultsFile, $line); } /** * @param string $key ODT API key. * @param string $secret ODT API secret. * @param string $action ODT API action - see ODT API documentation. * @param array $postData ODT API action data - see ODT API documentation. * @return array [bool $success, string $data] $success is true, if the operation succeeded, in this case $data represents the returned result. If the operation failed, $success is false and $data contains error message. */ function odtSendRequest($key, $secret, $action, array $postData) { // generate POST data string $postFields = http_build_query($postData); $dateTime = new \DateTime('now', new \DateTimeZone('UTC')); $time = $dateTime->format('Y-m-d H:i:s'); $message = $key . $time . $postFields; $signature = hash_hmac('sha512', $message, $secret); // generate extra headers $headers = array( 'Sign: ' . $signature, 'Time: ' . $time, 'Key: ' . $key ); $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, 'https://secured.online-domain-tools.com/api/user/' . $action . '/'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_TIMEOUT, 240); $data = curl_exec($ch); if ($data === false) { $error = curl_error($ch); $result = array(false, $error); } else { $result = array(true, $data); } curl_close($ch); return $result; } /** * This function is called during the inital request that starts the job. * It verifies that we have access rights to all files that we need. * * @return bool Returns true if succeeded, false otherwise. */ function initialization() { $result = false; global $whoisInputFile, $whoisResultsFile, $logFile; // Clean log file. $res = file_put_contents($logFile, ""); if ($res !== false) { mlog("initialization()"); // Check input is readable. $inputContents = file_get_contents($whoisInputFile); if ($inputContents !== false) { // Check input is writeable. $res = file_put_contents($whoisInputFile, $inputContents); if ($res !== false) { // Clean output. $res = file_put_contents($whoisResultsFile, ""); if ($res !== false) { $result = true; } else mlog("initialization(): ERROR: Unable to write to output file " . "'$whoisResultsFile'."); } else mlog("initialization(): ERROR: Unable to write to input file '$whoisInputFile'."); } else mlog("initialization(): ERROR: Unable to read input file '$whoisInputFile'."); } else mlog("initialization(): ERROR: Unable to write to log file '$logFile'."); mlog("initialization(-):" . (int)$result); return $result; } /** * Checks if there are any incoming data in form of JSON POST. * If not, does nothing and returns immediately. If yes, the data are processed. * * @return bool Returns true if succeeded, false otherwise. */ function processIncomingData() { $result = false; if (!isset($_GET["query"])) $result = initialization(); mlog("processIncomingData()"); if (isset($_GET["query"])) { $query = $_GET["query"]; mlog("processIncomingData(): Query is '$query'.\n"); // Load data from input. $json = file_get_contents('php://input'); if ($json) { $jsonLen = strlen($json); mlog("processIncomingData(): Incoming data ($jsonLen bytes) detected " . "(first 256 bytes):\n" . substr($json, 0, 256) . "\n"); $response = json_decode($json, true); if ($response !== null) { if ($response['success'] == 1) { mlog("processIncomingData(): Call succeeded."); } else { mlog("processIncomingData(): ERROR: Call failed with error message: '" . $response['message'] . "'"); } writeOutput($query . "::" . $json); $result = true; } else mlog("processIncomingData(): ERROR: Call failed. Server response is not a valid" . " JSON message."); } else mlog("processIncomingData(): ERROR: Call failed. No JSON POST data received."); } else mlog("processIncomingData(): No incoming data. Initialization called."); mlog("processIncomingData(-):" . (int)$result); return $result; } /** * Removes and returns first line from the input file. * * @return string Returns first line of the input file if succeeded, false otherwise. */ function removeLineFromInputFile() { mlog("removeLineFromInputFile()"); global $whoisInputFile; $result = false; $lines = file($whoisInputFile, FILE_IGNORE_NEW_LINES); if ($lines !== false) { if (count($lines) > 0) { $firstLine = $lines[0]; $newLines = array_slice($lines, 1); $newContent = implode("\n", $newLines); $res = file_put_contents($whoisInputFile, $newContent); if ($res !== false) { $result = trim($firstLine); if (empty($result)) $result = false; } else mlog("removeLineFromInputFile(): ERROR: Unable to write to file" . " '$whoisInputFile'."); } else mlog("removeLineFromInputFile(): Empty input file '$whoisInputFile'."); } else mlog("removeLineFromInputFile(): ERROR: Unable to read from file" . " '$whoisInputFile'."); mlog("removeLineFromInputFile(-):" . ($result !== false ? $result : "false")); return $result; } // Return what is expected by API. echo "ODT: OK\n"; // If processing is OK, continue. if (processIncomingData()) { $done = false; while (!$done) { // Load query from input file. $query = removeLineFromInputFile(); if ($query !== false) { $arg = "query=" . urlencode($query) . "&token=" . urlencode($token); $url = strtok($thisUrl, '?') . "?$arg"; mlog("Callback URL set to '$url'."); // Send request via ODT API. list($isHttpRequestOk, $result) = odtSendRequest($apiKey, $apiSecret, "tool/whois/query", array( "query" => $query, "asyncCallback" => $url )); if ($isHttpRequestOk) { $response = json_decode($result, true); if ($response !== null) { if ($response['success'] == 1) { mlog("API call succeeded, waiting for callback ..."); $done = true; } else { mlog("ERROR: API call failed with error: " . $response['message']); writeOutput($query . "::" . $result); } } else mlog("ERROR: API call failed. Server response is not a valid JSON message."); } else mlog("ERROR: HTTP request failed with error: " . $result . "."); } else { mlog("No more queries to process, all done!"); $done = true; } } } else mlog("ERROR: Processing incoming data failed, stopping execution.");