How can SSL verification be properly handled when using cURL in PHP scripts?
When using cURL in PHP scripts, SSL verification can be properly handled by setting the CURLOPT_SSL_VERIFYPEER option to true and the CURLOPT_CAINFO option to the path of a certificate authority file. This ensures that cURL verifies the SSL certificate of the server it is connecting to, providing secure communication.
<?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_CAINFO, '/path/to/certificate.pem');
$response = curl_exec($ch);
if($response === false){
echo 'cURL error: ' . curl_error($ch);
}
curl_close($ch);
echo $response;
?>
Keywords
Related Questions
- What are common issues when setting up a PHP server on Windows Server 2008 R2 with IIS?
- What security considerations should be taken into account when extracting and displaying content from external sources on a website using PHP?
- What are the potential pitfalls or errors that may occur when using mod_rewrite in PHP?