Whois 使用 fsockopen 而不是使用 curl

Whois works with fsockopen not with curl

这个有效:

$connection = fsockopen("whois.iis.se", 43);
fputs($connection, "google.se\r\n");
while (!feof($connection)) {
    $data .= fgets($connection, 4096);
}
fclose($connection);
echo nl2br($data);

但这不起作用:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "whois.iis.se");
curl_setopt($ch, CURLOPT_PORT, 43);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "google.se\r\n");
$data = curl_exec($ch); 
curl_close($ch);
echo nl2br($data);

这个 curl 函数有什么问题?

我知道这是一岁但是:接受的答案不正确。 cURL 实际上确实具有支持 Whois 协议的功能,但它需要的不仅仅是一个简单的请求。

我不知道 PHP 的确切翻译,但我认为这只需要更改选项以支持 TELNET,因为这就是 Whois 使用的内容:

curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_TELNET);

最后,一旦您收到回复,您可能需要处理引荐,除非您访问的是具有实际 WhoIs 数据的直接 Whois 服务器。对于示例中的 Whois 服务器,它应该 return mot 地址作为对 google.se.

的响应

这个支持已经存在很长时间了,我相信甚至在这个问题被问到之前。


更新:

抱歉,还没有真正使用 PHP 和 PHP 的 CURL,但得到了以下工作。这不是理想的,但不确定处理 stdin 东西的最佳方法。

$filename = "query.txt";
$fp = fopen($filename, "r");

function readFunc($ch, $fh, $length = false) {
  return fread($fh, $length);
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "telnet://whois.iis.se:43");
curl_setopt($ch, CURLOPT_PORT, 43);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_NOPROGRESS, TRUE);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_TELNET);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filename));
curl_setopt($ch, CURLOPT_READFUNCTION, 'readFunc');
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);  // Can take this out

$data = curl_exec($ch);
curl_close($ch);
echo nl2br($data);

然后returns:

$ php query.php
* Rebuilt URL to: telnet://whois.iis.se:43/
*   Trying 91.226.37.83...
* TCP_NODELAY set
* Connected to whois.iis.se (91.226.37.83) port 43 (#0)
* Closing connection 0
# Copyright (c) 1997- IIS (The Internet Foundation In Sweden).<br />
# All rights reserved.<br />
# The information obtained through searches, or otherwise, is protected<br />
# by the Swedish Copyright Act (1960:729) and international conventions.<br />
# It is also subject to database protection according to the Swedish<br />
# Copyright Act.<br />
# Any use of this material to target advertising or<br />
# similar activities is forbidden and will be prosecuted.<br />
# If any of the information below is transferred to a third<br />
# party, it must be done in its entirety. This server must<br />
# not be used as a backend for a search engine.<br />
# Result of search for registered domain names under<br />
# the .se top level domain.<br />
# This whois printout is printed with UTF-8 encoding.<br />
#<br />
state:            active<br />
domain:           google.se<br />
holder:           mmr8008-53808<br />
admin-c:          -<br />
tech-c:           -<br />
billing-c:        -<br />
created:          2008-10-20<br />
modified:         2016-09-18<br />
expires:          2017-10-20<br />
transferred:      2009-03-06<br />
nserver:          ns1.google.com<br />
nserver:          ns2.google.com<br />
nserver:          ns3.google.com<br />
nserver:          ns4.google.com<br />
dnssec:           unsigned delegation<br />
status:           serverDeleteProhibited<br />
status:           serverTransferProhibited<br />
status:           serverUpdateProhibited<br />
registrar:        MarkMonitor Inc<br />

如果您需要它来处理随机字符串,那么您可以轻松地写入一个临时文件并使用它来传递给函数。不幸的是,我没有时间继续研究 PHP curl 选项以确定哪个选项最适合将数据流式传输到 WHOIS 服务器。

文件 query.txt 中只有 google.se