SSL peer certificate or SSH remote key was not OK
When you see the error SSL peer certificate or SSH remote key was not OK while running a cURL request from PHP, it is almost always an issue of trust verification, not a misconfiguration of your web server. It is entirely possible for your Apache VirtualHost to serve HTTPS correctly and pass validation in a browser, yet fail immediately when hit by a script. This discrepancy stems from how the operating system and the cURL library handle certificate chains differently than a modern web browser.
The error message indicates that the client cannot validate the identity of the server it is connecting to. In your specific case, you are attempting to bypass this check by setting CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST to false. While this resolves the immediate crash, it exposes your application to significant security risks. Disabling peer verification means your PHP script will accept any certificate presented by the server, including those issued by attackers or self-signed certificates intended for other services. This effectively disables one of the primary mechanisms preventing man-in-the-middle attacks.
To resolve this without disabling security checks, you must understand why the validation fails in the first place. The most common cause is that the system executing your PHP script lacks the Certificate Authority (CA) root certificates required to validate your server's chain. Even if your Apache server presents a valid certificate signed by Let's Encrypt or DigiCert, the underlying OS might not have the corresponding root store populated, or it might be using an outdated bundle.
You can diagnose this by checking which CA bundle cURL is actually attempting to use. On Linux systems, you can inspect the environment variable SSL_CERT_FILE or check if the system defaults are being used:
echo $SSL_CERT_FILE
ls -la /etc/ssl/certs/ca-certificates.crt
If these paths do not exist or point to an empty file, cURL cannot perform validation. The correct approach is to provide a valid CA bundle explicitly via the CURLOPT_CAINFO option, which you are already attempting to do in your code. However, ensure that $path points to a file containing a complete chain of trusted root certificates. You can obtain an updated bundle from your package manager:
sudo apt-get update
sudo apt-get install ca-certificates
On macOS or systems using OpenSSL directly, you might need to point cURL to the system store or an updated ca-bundle.crt file.
Another frequent pitfall involves mixed environments where the script runs under a specific user context that does not have read permissions for the standard certificate directories. Ensure the PHP process has read access to the CA bundle you are specifying.
There is also the matter of certificate pinning or hostname mismatches. If your server certificate was issued for myVHost but your cURL request targets a different domain, validation will fail regardless of the CA bundle. You can debug this by using OpenSSL from the command line to simulate the connection and see the specific error:
curl -vI https://myVHost --cacert /path/to/ca-bundle.crt
Look closely at the output following the certificate chain display. If you see unable to get local issuer certificate, your CA bundle is missing a root. If you see hostname mismatch, the certificate domain does not match the URL in your cURL request.
Disabling verification with $opts[CURLOPT_SSL_VERIFYPEER] = 0 should be considered a temporary debugging step only. In production environments, maintaining secure communication channels is non-negotiable. If you must disable peer verification due to legacy infrastructure issues, ensure that the connection is protected by TLS and that you are using mutual authentication (client certificates) if possible, though this adds complexity. Ideally, focus on updating your system's CA store or bundling a trusted root certificate into your deployment artifact so that CURLOPT_SSL_VERIFYPEER can remain enabled, securing your API calls against impersonation attacks.