Fix cURL Error: SSL Certificate Problem — Unable to Get Local Issuer Certificate

Fix cURL error: SSL certificate problem: unable to get local issuer certificate

I was grabbing data from a site with a PHP script that uses cURL, and every request failed with this warning:

SSL certificate problem: unable to get local issuer certificate

The problem comes from a misconfigured server on the other side: it doesn’t send the full certificate chain. Here is how to find out what is missing and how to fix it on the machine that makes the requests.

Problem: HTTPS requests with cURL (from the command line, a PHP script or wp_remote_get()) fail with “SSL certificate problem: unable to get local issuer certificate”.

Why it happens

A site’s certificate is signed by an intermediate certificate, which in turn is signed by a root certificate that your system trusts. The server is supposed to send its own certificate together with the intermediate one. If it sends only its own, browsers often cope (they cache intermediates or download them), but cURL can’t build the chain to a trusted root and refuses the connection.

Step 1. Inspect the certificate chain

Run this in a terminal, replacing www.yoursite.com with the domain you are requesting:

openssl s_client -connect www.yoursite.com:443 -servername www.yoursite.com

Look for these lines in the output:

depth=0 OU = Domain Control Validated, CN = *.yoursite.com
verify error:num=20:unable to get local issuer certificate
verify error:num=21:unable to verify the first certificate
---
Certificate chain
 0 s:/OU=Domain Control Validated/CN=*.yoursite.com
   i:/C=US/ST=Arizona/L=Scottsdale/O=GoDaddy.com, Inc./OU=http://certs.godaddy.com/repository//CN=Go Daddy Secure Certificate Authority - G2

The chain has only one certificate (number 0), and the i: line shows who issued it: here, GoDaddy’s intermediate certificate “Go Daddy Secure Certificate Authority – G2”.

Step 2. If it’s your server: install the full chain

If the site belongs to you or your client, this is the real fix. In the web server configuration, use the certificate file that includes the intermediate certificates (the “full chain” or “bundle” file from the certificate authority). After that, every client, cURL included, can verify the site.

Step 3. If it’s someone else’s server: trust the intermediate locally

If you can’t change the remote server, add the missing intermediate certificate to the trusted store on your machine. Download it from the issuer’s repository, the address from the i: line. For GoDaddy G2 that is gdig2.crt.pem from certs.godaddy.com/repository.

On Debian and Ubuntu, put it into the local certificates folder and rebuild the store:

sudo cp gdig2.crt.pem /usr/local/share/ca-certificates/gdig2.crt
sudo update-ca-certificates

(On older systems the same was done by copying the file to /etc/ssl/certs/ and running c_rehash.)

Run the openssl s_client command from Step 1 again. If the output now shows three levels (depth=2, depth=1, depth=0), each with verify return:1 and no error lines, the chain is complete, and the script works.

WordPress has its own certificate bundle

wp_remote_get() and the other WordPress HTTP functions don’t use the system store: they verify certificates against wp-includes/certificates/ca-bundle.crt. A certificate added to the system doesn’t help them. To use the system bundle for WordPress requests, point the sslcertificates argument to it:

add_filter( 'http_request_args', 'helloadmin_system_ca_bundle' );
function helloadmin_system_ca_bundle( $args ) {
    $args['sslcertificates'] = '/etc/ssl/certs/ca-certificates.crt'; // Debian/Ubuntu path
    return $args;
}

NOTE: Never “fix” this by disabling verification (CURLOPT_SSL_VERIFYPEER = false or 'sslverify' => false). It makes the error disappear, but then anyone between you and the server can read and change the traffic.