Report this

What is the reason for this report?

PHP cURL Fails to Establish Secure Connection with Custom CA Certificates

Posted on May 27, 2023

Hello everyone,

I am facing a challenging issue with a PHP application that involves cURL and SSL. The application is designed to connect to a remote server over HTTPS. This server employs a certificate issued by a custom internal Certificate Authority (CA) rather than a well-known public CA.

When I try accessing the server through a web browser after adding the custom CA certificate to the system trust store, it works fine. However, when I attempt to connect using my PHP script via cURL, I’m met with an SSL certificate problem.

Here is a snippet of the PHP code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://my-secure-server.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CAINFO, '/path/to/custom/ca_certificate.pem');
$response = curl_exec($ch);

if($response === false){
    echo 'Error: ' . curl_error($ch);
}

curl_close($ch);

The error I keep getting is:

Error: SSL certificate problem: unable to get local issuer certificate

I’ve confirmed that the path to the CA certificate file is correct, the file is readable by the PHP process, and its format is correct (PEM encoded).

I’ve also attempted to use CURLOPT_CAPATH pointing to the directory of the certificate, but the result is the same. Additionally, I’ve checked that openssl.cafile and curl.cainfo are properly set in my php.ini file.

Could anyone offer some guidance on why my PHP application is not recognizing the SSL certificate even though it’s valid and trusted by the system? I’m using PHP 7.4, cURL 7.68, and OpenSSL 1.1.1.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.