9

I need to get whois information. My function is working fine, but isn't returning "Administrative Contact, Registrant Contact, Administrative Contact, Technical Contact" information.

But when I run following command on my Mac it returns all information "whois google.com"

Here is my php function to get information from whois server

function QueryWhoisServer($whoisserver, $domain) {
$port = 43;
$timeout = 10;
$fp = @fsockopen($whoisserver, $port, $errno, $errstr, $timeout) or die("Socket Error " . $errno . " - " . $errstr);

fputs($fp, $domain . "\r\n");
$out = "";
while(!feof($fp)){
    $out .= fgets($fp);
}

fclose($fp);
return $out; 
} 
echo QueryWhoisServer("whois.verisign-grs.com", "google.com");
Anthony
  • 36,459
  • 25
  • 97
  • 163
Anakbhai Gida
  • 668
  • 3
  • 20
  • 5
    This is not a problem with your code but with the WHOIS server. WHOIS is not required to send contacts information. It's just missing from the server response. No issue with the code you've shared. – Patrick Moore Apr 12 '18 at 18:20
  • But when i run "whois google.com" on command line it will show all information and also this page showing all information https://www.whois.com/whois/google.com – Anakbhai Gida Apr 12 '18 at 18:22
  • 2
    I don't know which source Mac uses by default, but if you try running command to specify using `whois.verisign-grs.com` (same as your code), my guess is you'll see identical results to what your PHP code shows. Try this on the command line: `whois -h whois.verisign-grs.com google.com` – Patrick Moore Apr 12 '18 at 18:27
  • 1
    Then just use ``echo `whois google.com`;`` instead. – Lawrence Cherone Apr 12 '18 at 18:30
  • "whois -h whois.verisign-grs.com google.com" this will return same result but also what about https://www.whois.com/whois/google.com they are also showing all informations – Anakbhai Gida Apr 12 '18 at 18:34
  • Also https://whoapi.com/ return all the contact information – Anakbhai Gida Apr 12 '18 at 18:36
  • 1
    But this is totally different service providing this information. whois.verisign-grs.com is not same as whois.com or whoapi.com. So use your same code will not work for the others, because they are not providing WHOIS server on port 43, but instead web request or API request on port 80/443. Completely different. So, instead, look at other solutions already out there https://stackoverflow.com/questions/6728143/how-do-i-run-a-whois-lookup-with-php-or-python – Patrick Moore Apr 12 '18 at 18:44
  • 1
    @PatrickMoore " WHOIS is not required to send contacts information." on the contrary this IS exactly the purpose of the whois protocol. Due to regulations such as the GDPR the registries may start showing less data but this is a policy decision not a technical limitation. And when they do it is for all clients, the result will not changed based on your source IP. – Patrick Mevzek Apr 14 '18 at 04:02
  • @PatrickMoore I have tried your given link but still not able to get solution for my question that is how i get whois "Administrative Contact, Registrant Contact, Administrative Contact, Technical Contact" Infomation using php code and if whois server not return that information then how websites like (https://www.whois.com/whois/google.com) showing contact information – Anakbhai Gida Apr 14 '18 at 17:43
  • 1
    Just use [`PHPWhois`](https://sourceforge.net/projects/phpwhois/files/phpwhois) – Code4R7 Apr 16 '18 at 19:17
  • @Code4R7 That is also not returning contact information data. Actually i have tried that all :) – Anakbhai Gida Apr 16 '18 at 19:24
  • Run this from terminal : `telnet whois.verisign-grs.com 43` and enter google.com. You will see that those contact fields are not provided. It isn't your code, it is that whois server (like everyone else has said). Just because some servers provide that info doesn't mean they all will. Why are you only using `whois.verisign-grs.com` when there are other whois servers that you know provide the info you are wanting? – Anthony Apr 19 '18 at 16:25
  • There are a lot of projects that already do what you are wanting. Here's one https://github.com/phpWhois/phpWhois – Anthony Apr 19 '18 at 16:31

1 Answers1

10

I found a possible solution, assuming (as checked by myself) registrar whois servers are returning contact infos.

To do this, it is needed for each domain to query related registrar whois server as per code below.

Check code comments for little explanation of what each function does.

function GetWhoisInfo($whoisserver, $domain){
  $port = 43;
  $timeout = 10;
  $fp = @fsockopen($whoisserver, $port, $errno, $errstr, $timeout) or die("Socket Error " . $errno . " - " . $errstr);
  stream_set_blocking($fp, true);
  fputs($fp, $domain . "\r\n");
  $out = "";

  while(!feof($fp)){
      $out .= fgets($fp);       
  }

  fclose($fp);  
  return $out;
}

function GetRegistrarWhoisServer($whoisserver, $domain) {
  $out = GetWhoisInfo($whoisserver, $domain);
  $rws_string = explode("\r\n", $out);
  $rws = explode("Registrar WHOIS Server: ", $rws_string[2])[1];  
  return $rws; 
}

function WhoisToJson($winfo) {
  $winfoarr = explode(PHP_EOL, $winfo);
  $jsonarr = [];
  foreach($winfoarr as $info){
   $infodata = explode(": ", $info);
   if($infodata[0] !== "")$jsonarr[$infodata[0]] = $infodata[1];    
   //avoid to process privacy info at the end of whois service output
   if($infodata[0] === "DNSSEC")break;
  }
  return json_encode($jsonarr);
} 

function QueryWhoisServer($whoisserver, $domain) {
  //query to $whoisserver whois to get registrar whois server address only
  $rws = GetRegistrarWhoisServer($whoisserver, $domain);

  //query to registrar whois server (registrar whois servers are returning contact infos)
  $out = GetWhoisInfo($rws, $domain);  

  //parsing infos and formatting to json
  return WhoisToJson($out);
} 

echo QueryWhoisServer("whois.verisign-grs.com", "google.com");
Giacomo Penuti
  • 1,028
  • 11
  • 14
  • you should really set blocking mode on those connections, else the while loops will use a shitton of cpu (~100% of 1 core) – hanshenrik Apr 18 '18 at 14:32
  • @hanshenrik Thanks for suggestion, blocking mode added to GetWhoisInfo function. This way fgets won't return unless there is content available on stream (should prevent loops if nothing to add to $out). In this case, anyway, i tested the performance improvement adding a counter to while loop and setting "blocking mode" vs "not blocking mode" is not changing the loop iterations number. `$i = 0; while(!feof($fp)){ $out .= fgets($fp); $i++; } echo $i;` Can anyone give further explanation about why does that happen? – Giacomo Penuti Apr 19 '18 at 08:58
  • @GiacomoPenuti Your answer looks correct to get contact information for any domain but still WhoisToJson() function only convert first line to JSON – Anakbhai Gida Apr 19 '18 at 16:52
  • @GiacomoPenuti answer is correct. The original question was to get back all information from a whois database using a php script. His code works! – Mike Stratton Apr 20 '18 at 06:04
  • 1
    @AnakbhaiGida Thanks for your review. The function, according to your request, returns a json text output containing all informations about domain (in my enviroment). I made a version with few edits in order to test it (pretty printed json and formatted output), and i think it's fine. Here is the version for testing purposes: http://phpfiddle.org/lite/code/4egp-20d8 . Of course for performance reason i woudn't pretty print json output in production so the output of WhoisToJson function is one json "long" line. Let me know if any modification is required to suite the question. – Giacomo Penuti Apr 20 '18 at 08:56
  • 1
    Thanks all for helping – Anakbhai Gida Apr 20 '18 at 09:05