The Linux Page

stream_socket_client() fails with no errors...

Old Directory with missing and broken directions.

Today I had a weird one.

The PHP function named stream_socket_client(), used to create a socket to communicate with another computer over a network, may return $errno = 0 and $errstr = "".

The PHP reference actually mentions the problem:

On failure the errno and errstr arguments will be populated with the actual system level error that occurred in the system-level connect() call. If the value returned in errno is 0 and the function returned FALSE, it is an indication that the error occurred before the connect() call. This is most likely due to a problem initializing the socket. Note that the errno and errstr arguments will always be passed by reference.

As we can see, they say clearly that the function may return false without setting the error number or message. This is generally because an initialization failed. They just don't tell you what failed... and no error in the error logs either. Nothing.

So this is a difficult one to debug since you could have all sorts of reasons why that call fails the initialization.

Maybe you have an invalid $context parameter.

It could be that you need to specify where the SSL certificate are found on your computer (really that should be automatic...)

Or in my case, it was the protocol used that was not supported on the platform where PHP eFax was installed. That platform (RedHat EL7) did not understand the "tls://" protocol. Instead, it wanted to see "ssl://". Oh well! Talk about walking in the fog.

Some people also reported having problems because a certificate was missing.

Either way, if you have such an error, you'll have to test everything... until it works. Since there is no real feedback, you won't be able to do much else. Someone mentioned the possibility to use a trace and see where the error occurs, but I have not tested that. (I'm not too sure how you can do that and I don't see how that could work since the stream function is in C, not a PHP class.)

As mentioned in a comment, you may also want to check your firewall. Obviously, if you block the port or IP address, it won't work as expected. The nc tool may help you to verify that you can connect to whatever you are trying to connect to.

If you ran in the same situation and found yet a different solution to fix the problem, please comment below with your solution to help others with this one! Thank you.

Re: stream_socket_client() fails with no errors...

Excellent! I'm glad it helped you. I added a note in my post about that possibility and a way to check connections with a modern tool (a.k.a. "nc" instead of the usual "telnet")

Re: stream_socket_client() fails with no errors...

There may be a NUMBER of problems that may cause an error.

In my case I had to allow the target IP in the firewall AND I had to use 'ssl://' protocol instead of 'tls://'. (In the latter case, the article helped me, so thank you!)

Anyway, it is still hard to debug, but my advice: always begin with the firewall. ;)