How to handle SSL/TLS certificate verification with cURL in PHP?

To handle SSL/TLS certificate verification with cURL in PHP, you can follow the steps below:

  1. Set the CURLOPT_SSL_VERIFYPEER option to true to enable SSL certificate verification.
  2. Set the CURLOPT_CAINFO option to the path of the CA certificate bundle file or the specific certificate file you want to use for verification.
  3. (Optional) If you don't have a CA certificate bundle file, you can disable verification for development purposes by setting CURLOPT_SSL_VERIFYPEER to false. However, this is not recommended for production environments.
  4. (Optional) If you encounter issues with SSL certificate verification, you can set the CURLOPT_SSL_VERIFYHOST option to false to disable hostname verification. Again, this is not recommended for production environments.

Here's a code example illustrating how to handle SSL/TLS certificate verification with cURL in PHP:

$url = 'https://example.com/api'; $ch = curl_init($url); // Enable SSL certificate verification curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // Set the CA certificate bundle file path curl_setopt($ch, CURLOPT_CAINFO, '/path/to/ca_certificate_bundle.crt'); // (Optional) Disable SSL certificate verification (not recommended for production) // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // (Optional) Disable hostname verification (not recommended for production) // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // Additional cURL options and request configurations // ... // Execute the cURL request $response = curl_exec($ch); // Check for errors if(curl_errno($ch)){ echo 'cURL error: ' . curl_error($ch); } // Close the cURL session curl_close($ch);

Note: Make sure to replace the '/path/to/ca_certificate_bundle.crt' with the actual path to your CA certificate bundle file.