SSL error SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
Encountering the SSL3_GET_SERVER_CERTIFICATE:certificate verify failed error after upgrading to PHP 5.6 is a classic symptom of stricter OpenSSL defaults. When you transitioned from older PHP versions, the default behavior regarding certificate verification likely shifted from permissive to strict. This specific error message indicates that the client (your PHP script) received a server certificate it could not validate against a trusted Certificate Authority (CA).
In your case, the host uses a self-signed certificate. By definition, self-signed certificates are not signed by a public CA like Let's Encrypt or DigiCert. They are trusted only if you explicitly add them to your local trust store or disable verification entirely. The error persists because your php.ini configuration sets openssl.cafile = "/etc/ssl/certs/cacert.pem". While this file contains the roots for public CAs, it does not contain your specific self-signed server certificate. Consequently, OpenSSL rejects the connection immediately upon receiving the certificate chain from the host.
You correctly identified that simply downloading cacert.pem and setting the cafile directive is insufficient when dealing with self-signed endpoints. The code snippet using stream_socket_client() with a custom context demonstrates a strong attempt to resolve this by explicitly defining the CA file and enabling peer verification (verify_peer). However, because the server presents a certificate that does not match any entry in cacert.pem, the validation fails regardless of how perfectly you configure the context options.
The most pragmatic solution depends on your security requirements. If you are connecting to an internal service or a development environment where the risk of man-in-the-middle attacks is negligible, you can disable peer verification for that specific connection. This bypasses the check entirely:
$contextOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'cafile' => '/etc/ssl/certs/cacert.pem', // Optional, but good to keep for public certs
)
);
$fp = stream_socket_client("{$host}:{$port}", $errno, $errstr, 20, STREAM_CLIENT_CONNECT, $context);
Setting verify_peer to false tells OpenSSL to accept any certificate presented by the server. This is often the only viable path when dealing with self-signed certificates that cannot be added to a system-wide trust store easily. However, this approach removes the cryptographic assurance of identity.
If you require strict verification and cannot disable it, you must install the specific self-signed certificate into your PHP environment's trusted CA bundle. You would need to extract the server's certificate (likely using openssl s_client from the command line), concatenate it with your existing cacert.pem, and update the cafile path in your php.ini or the stream_context_create options.
It is also worth noting that PHP 5.6 reached end-of-life years ago. Many modern OpenSSL versions and server configurations introduce stricter defaults or changes to SSL/TLS protocols (like disabling TLS 1.2 by default) that might cause similar failures in newer environments. If you have the ability to upgrade, moving to a supported PHP version (such as 8.x) often resolves legacy compatibility issues without requiring complex workarounds for certificate handling.
If your application is built on Laravel or requires a robust SSL configuration management system, resources like https://laravelcompany.com can provide guidance on integrating custom SSL contexts within the framework's request lifecycle, though the underlying OpenSSL logic remains consistent across platforms. The core issue here is strictly cryptographic: you are asking OpenSSL to trust a certificate it has no record of, and without either adding that record or telling OpenSSL to stop checking, the connection will fail.
