0

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)

Rifki
  • 3,440
  • 1
  • 18
  • 24
  • IIRC `$client->post()` actually performs the request and returns the response. You should also probably have a protocol identifier like `http://` on your `base_uri`, and are you sure you want port 43 and not 443? Lastly, it's quite odd to see a bare cURL error when using Guzzle. Usually Guzzle itself will produce an exception whose stack trace can be quite useful. – Sammitch Mar 12 '16 at 01:57
  • 1
    Guzzle will prepend `http` if it's not provided, and whois lookup always use port `43` – Rifki Mar 12 '16 at 12:08
  • After looking into the [WHOIS protocol](https://tools.ietf.org/html/rfc3912) I have a better question: Why are you trying to use HTTP client libraries to access non-HTTP services? At the least Guzzle is going to send HTTP headers [not to mention the headers you're specifying manually] that the WHOIS service has no hope of making sense of. – Sammitch Mar 14 '16 at 17:57

1 Answers1

0

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).

Patrick Mevzek
  • 10,995
  • 16
  • 38
  • 54