【问题标题】:Whois works with fsockopen not with curlWhois 适用于 fsockopen 而不是 curl
【发布时间】:2017-05-16 18:27:55
【问题描述】:

这行得通:

$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 函数有什么问题?

【问题讨论】:

    标签: php curl whois


    【解决方案1】:

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

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

    curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_TELNET);
    

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

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


    更新:

    抱歉,没有真正使用 PHP 和 PHP 的 CURL,但可以使用以下方法。这并不理想,但不确定处理标准输入内容的最佳方式。

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

    然后返回:

    $ 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

    【讨论】:

    • curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_TELNET);不要接缝为 google.se 工作
    • 查看有关如何让TELNET 工作的问题的更新。
    • 我为您的详细信息 +1 您的答案,但我仍然认为使用基本的 HTTP 库连接到 whois 服务器是一个错误的想法,因为这些服务器非常简单地可用作 TCP 套接字在端口 43 上,因此他们不需要复杂 HTTP 库中的所有技巧。还有专门用于执行 whois 查询的 PHP 库。
    • 只是一个注释。如果这是在共享托管服务器上,则此方法将不起作用。出于安全考虑,大多数共享主机帐户都不允许打开端口。对于共享托管服务器,您应该尝试 WhoIs API,它将以 XML 或 JSON 形式返回 whois 查找数据。更多信息请参阅...whois.arin.net/ui
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 2011-06-23
    • 2015-06-05
    相关资源
    最近更新 更多