What are the best practices for handling SSL certificates and HTTPS requests in PHP when using cURL?

When making HTTPS requests in PHP using cURL, it is important to handle SSL certificates properly to ensure secure communication. One common issue is SSL certificate verification errors, which can be resolved by setting the appropriate cURL options to verify the peer and host. Additionally, you can specify the path to a CA certificate bundle to use for verification.

// Initialize cURL session
$ch = curl_init();

// Set cURL options for HTTPS request with SSL certificate verification
curl_setopt($ch, CURLOPT_URL, 'https://example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, '/path/to/ca-bundle.crt');

// Execute cURL request
$response = curl_exec($ch);

// Check for errors
if(curl_errno($ch)) {
    echo 'cURL error: ' . curl_error($ch);
}

// Close cURL session
curl_close($ch);

// Process the response
echo $response;