PHP cURL to Guzzle


PHP cURL to Guzzle

是否可以将此cURL代码转换为Guzzle?

$ch = curl_init('whois.nic.co');
curl_setopt($ch, CURLOPT_PORT, 43);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "example.co'r'n");
$response = curl_exec($ch);
curl_close($ch);

尝试使用此代码,但似乎不起作用。

$client   = new Client(['base_uri' => 'whois.nic.co:43']);
$request  = $client->post('', array('Content-Type' => 'text/plain; charset=UTF8'), "example.co'r'n");
$response = $request->send();

上面的代码返回错误:cURL error 0: The cURL request was retried 3 times and did not succeed. The most likely reason for the failure is that cURL was unable to rewind the body of the request and subsequent retries resulted in the same error. Turn on the debug option to see what went wrong. See https://bugs.php.net/bug.php?id=47204 for more information. (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

正如@Sammitch所写,whois运行在TCP而不是HTTP(虽然你可以在网上找到服务,通过网站或HTTP API为你提供whois服务,但这不是原始的whois协议)。

在端口 43 上,客户端基本上应该给出一行查询,服务器回复一团非结构化的文本。这就是协议定义的全部内容。所以没有像HTTP/1.0及更高版本那样的标头。

因此,不要尝试使用 HTTP 客户端来执行请求。它有时可以承受巨大的扭曲,你可以在这里找到另一个关于curl的例子:https://stackoverflow.com/a/45286777/6368697但归根结底,这样做而不是纯粹的 telnet 连接或使用专门研究 whois 的语言库是没有真正意义的(主要是如果您需要对结果进行"高级"解析)。