How can PHP developers ensure that their code using cURL to access secure resources is properly configured and functioning correctly?

PHP developers can ensure that their code using cURL to access secure resources is properly configured and functioning correctly by setting the appropriate cURL options, including specifying the SSL version and verifying the peer. Additionally, they should handle any errors that may occur during the cURL request to ensure robust error handling.

<?php
$url = "https://example.com/api";
$ch = curl_init($url);
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/cacert.pem'); // Path to CA certificate bundle
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); // Specify SSL version
$response = curl_exec($ch);

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

curl_close($ch);
?>