amphp / socket

Non-blocking socket and TLS functionality for PHP based on Amp.
https://amphp.org/socket
MIT License
231 stars 38 forks source link

Replacing stream_socket_client with amphp/socket #83

Closed vivekrao1985 closed 2 years ago

vivekrao1985 commented 2 years ago

I currently have an array of options defined -

$options = [
    'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true,
    ],
];

I use the options to create a socket context -

$socket_context = stream_context_create($options);

And then I open a connection -

$connection = @stream_socket_client(
        "tcp://www.example.com:80",
        $errno,
        $errstr,
        0.5,
        STREAM_CLIENT_CONNECT,
        $socket_context
);

And I get a new resource that I can do stuff with. The above code works.

I want to port this code over to use amphp socket. So far I have this -

$clientContext = new ClientTlsContext("tcp://www.example.com");
$clientContext = $clientContext->withoutPeerVerification();
$connectContext = (new ConnectContext)->withTlsContext($clientContext)->withConnectTimeout(500);

$socket = connect("tcp://www.example.com:80", $connectContext);
$socket->onResolve(function ($fail, $success) {
    echo $fail->getMessage();
});

However I always get this error from the echo above -

All query attempts failed for www.example.com: Reading from the server failed, Reading from the server failed

What am I doing wrong?

kelunik commented 2 years ago

The error message reads like an dns error message, what's the class of the error object?

Also, please try to remove the tcp:// part from the ClientTlsContext constructor.

vivekrao1985 commented 2 years ago

Yes, this turned out to be a connectivity issue on my end. Once I resolved that everything worked. I did not need to remove the tcp:// part but it works either way.

kelunik commented 2 years ago

You have disabled peer verification anyway, so yeah, the hostname doesn't matter.