How do I run a WHOIS lookup with PHP or Python?

Viewed 22428

So anyways, I'm working on a small PHP website/script, and as one of the features I'd like to be able to run a WHOIS lookup on the current domain the PHP script is running on.

Ideally, it would be one function that I could call and in the function it would run the WHOIS, and then echo the results to the screen. It would take in the URL of the site to run the WHOIS lookup on, or it would just run it on the current URL/Domain (which is what I want), although I can feed it a variable for the website domain if need be.

I don't know much about WHOIS lookups (well, I know what they do, I just don't know how to run them in PHP), but I'd also be fine with having to query another website (even one of my own if you can give me the code for it).

Whatever works, please just let me know! The main thing is that, I'd prefer it to fit all in one function, and it definitely must fit in one PHP file/document.

6 Answers

To take Pavels answer one step further - this will break it down in to an array:

$whois = shell_exec("whois 45.118.135.255");

$result = explode("\n",$whois);

$out = array();
foreach ($result as $line){
    if (substr($line,0,1) == '%' || substr($line,0,1) == '#'){ continue; }

    $ps = explode(':',$line);
    $out[trim($ps[0])] = trim($ps[1]);
}

print '<pre>'; print_r($out); print '</pre>';

I found it here: https://whoisfreaks.com/documentation/api/whois-api.html. You can get Whoislook up by using PHP thru this code snippet:

     'https://api.whoisfreaks.com/v1.0/whois?whois=live&domainName=jfreaks.com&apikey=Your API_Key',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

I hope it'll work for you too.

Related