PHP cURL到Guzzle

问题描述 投票:0回答:1

是否可以将此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)

php curl guzzle whois
1个回答
0
投票

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

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

因此,不要尝试使用HTTP客户端来执行请求。它有时可以用很大的扭曲来工作,你可以在这里找到另一个例子来卷曲:https://stackoverflow.com/a/45286777/6368697但是在一天结束时没有真正意义这样做而不是纯粹的telnet连接或使用你的语言库专门用于whois(主要是如果你需要“高级”解析结果)。

© www.soinside.com 2019 - 2024. All rights reserved.