Trying to connect SSL server using fsockopen(): error 0


I’m running the next script from my local host and the production server, and Im getting different outputs. Anyone knows why am I getting that error from my localhost?

    $host = 'ssl://';
    $port = 993;
    $error = 0;
    $errorString = "";
    var_dump(fsockopen($host, $port, $error, $errorString, 30));

Local host output:

PHP Warning: fsockopen(): SSL operation failed with code 1. OpenSSL
Error messages: error:1416F086:SSL
routines:tls_process_server_certificate:certificate verify failed in
/tmp/test.php on line 7 PHP Warning: fsockopen(): Failed to enable
crypto in /tmp/test.php on line 7 PHP Warning: fsockopen(): unable to
connect to ssl:// (Unknown error) in /tmp/test.php
on line 7 bool(false) string(0) “” int(0)

Production server output:

resource(4) of type (stream)

sounds like your local server doesn’t have the certificate installed.

Basically, fsockopen is very low-level but without many options, or, arguably, “sane defaults”.

Instead, you can switch to stream_socket_client which will allow you to specify a context as the last parameter, and that object has many options, including a dedicated one with over a dozen options specific to SSL. The object created from this function is compatible with fwrite and other functions, so it should do everything you are hoping for

(a different command, 20 levels of context, and the Geek Squad won’t help if the certificate’s not installed. Just sayin.)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.