Error using PHP cURL with SSL certificates

Error using PHP cURL with SSL certificates

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Error using PHP cURL with SSL certificates

When integrating third-party authentication flows, you often encounter scenarios where a service requires both username/password credentials and a specific SSL certificate to establish trust. This is common in enterprise environments or legacy systems that have issued their own CA-signed certificates. If your shell commands work perfectly but your PHP script fails, the discrepancy almost always lies in how the OpenSSL library handles certificate file paths, not in the logic of your request itself.

The core issue stems from the difference between how command-line tools and the PHP runtime handle the CURLOPT_CAINFO option. In your terminal, using curl --cacert /path/to/servercert.cer explicitly tells the tool to validate the server's certificate chain against that specific file. However, when you pass this same path via CURLOPT_CAINFO in PHP, you encounter a specific error:

Error: error setting certificate verify locations: CAfile: /path/to/servercert.cer CApath: none

This error message is technically precise but often misunderstood. It indicates that the OpenSSL library expects either a single file containing the CA bundle or a directory structure if you are using CURLOPT_CAPATH. If the path provided is treated as a directory but contains no files, or if it is a single certificate that OpenSSL interprets incorrectly for verification purposes, the handshake fails.

The most reliable solution involves ensuring the certificate format and location match what the PHP runtime expects. While your command-line tool accepts the .cer file directly, the PHP configuration might require the path to be absolute and correctly formatted. If you are using a single CA bundle, CURLOPT_CAINFO should point to that file. However, if the error persists, it often suggests that the file is being interpreted as a directory by the underlying OpenSSL implementation in your specific environment.

To resolve this without disabling peer verification (which compromises security), you can adjust how the certificate is provided. One robust approach is to ensure the certificate file is treated strictly as a file containing the CA bundle. If you are using a custom certificate that acts as the root of trust, you must ensure it contains the full chain required for validation.

Consider this refined implementation strategy:

$handle = curl_init();

// Ensure we do not disable SSL verification
$options = array( 
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => ["Content-Type: application/json"],
    CURLOPT_FOLLOWLOCATION => false,
    // Do not set SSL_VERIFYHOST to 0 unless strictly necessary; default is 2.
    CURLOPT_SSL_VERIFYPEER => true, 
    // Point directly to the CA bundle file
    CURLOPT_CAINFO         => '/absolute/path/to/servercert.cer',
    CURLOPT_USERAGENT      => 'Mozilla/5.0 (compatible; PHP-CURL)',
    CURLOPT_URL            => 'https://website'
);

curl_setopt_array($handle, $options);

// Execute the request
$response = curl_exec($handle);

if ($response === false) {
    echo 'Error: ' . curl_error($handle);
} else {
    // Handle successful response
}

curl_close($handle);

A critical distinction exists between running this code via php test.php versus loading it through a web server like Apache or Nginx. When you run the script from the command line, PHP uses the system's default OpenSSL configuration, which often points to the standard /etc/ssl/certs/ca-certificates.crt file. If that system certificate doesn't match your specific requirement, the CLI might behave differently depending on how it initializes the SSL context, or conversely, work by coincidence if your environment has a global bundle installed that matches your needs.

When running via a browser (through Apache/Nginx), the PHP-FPM process or the web server module uses its own configured OpenSSL library and paths. These environments often have stricter isolation regarding certificate locations. If your web server is configured to use a different CA bundle, the relative path or the absolute path you provide in PHP might resolve differently than expected.

If you cannot change the file location, another viable pattern is to use CURLOPT_CAPATH instead of CURLOPT_CAINFO. This option expects a directory containing individual certificate files rather than a single bundle. You can create a temporary directory with your certificate and point to that:

$caPath = '/tmp/ca-bundle';
mkdir($caPath, 0755, true);
copy('/path/to/servercert.cer', $caPath . '/servercert.cer');

curl_setopt($handle, CURLOPT_CAPATH, $caPath);

This approach forces OpenSSL to scan the directory for trusted certificates. Note that you must ensure the directory has the correct permissions (usually 0755 or 0777) so the PHP process can read the files inside. Disabling peer verification via CURLOPT_SSL_VERIFYPEER => false should be a last resort; it bypasses the very mechanism allowing you to use the custom certificate for authentication, effectively nullifying the security benefit of providing the cert file in the first place.

If your application is built on Laravel or similar frameworks, ensure that the path to the certificate does not rely on relative paths like ./certs/cer. Web servers resolve relative paths differently depending on the document root and the current working directory of the PHP-FPM worker. Always use an absolute filesystem path when configuring CURLOPT_CAINFO or CURLOPT_CAPATH.

Our network