PHP域whois脚本不返回所有信息

时间:2018-04-12 18:13:43

标签: php api command-line-interface whois

我需要获取whois信息。我的功能正常,但没有返回“管理联系人,注册人联系人,管理联系人,技术联系人”信息。

但是当我在Mac上运行以下命令时,它会返回所有信息 “whois google.com”

这是我的php函数,用于从whois服务器获取信息

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");

1 个答案:

答案 0 :(得分:8)

我找到了一个可能的解决方案,假设(由我自己检查)注册商whois服务器正在返回联系信息。

为此,每个域都需要根据以下代码查询相关的注册商whois服务器。

检查代码注释,了解每个函数的功能。

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");