简体   繁体   中英

PHP cURL to Guzzle

Is it possible to convert this cURL code to 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);

Tried with this code but doesn't seems to be working.

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

The code above return error: 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) 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)

As written by @Sammitch, whois runs over TCP and not HTTP (while you can find services online that will provide you a whois service through a website or HTTP API, this is not the original whois protocol).

On port 43 the client is basically expected to give a one line query and the server replies with a blob of text, unstructured. This is all what the protocol defines. So no headers like in HTTP/1.0 and later.

So, do not try to use an HTTP client to do requests. It can work sometimes with huge contortions, you can find another example here for curl: https://stackoverflow.com/a/45286777/6368697 but at the end of the day it makes no real sense to do that instead of a pure telnet connection or using a library in your language specialized in whois (mostly if you need "advanced" parsing of the results).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM