Home > Article > Backend Development > Why Does My cURL HTTPS Connection Return Empty Content, and How Can I Fix It?
HTTPS connection failure problem and its solution
When using cURL to connect to an HTTPS website, I encountered a common problem: cURL only Returns 0-length content, only valid if cURL is set to return headers. This issue is most likely caused by HTTPS connections.
The workaround for this issue is to update the version of the authentication file that comes with cURL for validating HTTPS certificates. The latest authentication files can be downloaded from http://curl.haxx.se/ca/cacert.pem. Save it to a directory on your website and add in every request:
curl_setopt ($curl_ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
It is important to note that in the solution do not disable CURLOPT_VERIFYPEER and CURLOPT_VERIFYHOST, Because this will reduce the security of the code.
A better solution is to use the method proposed by Jasen as follows:
In php.ini add:
curl.cainfo=/etc/ssl/certs/ca-certificates.crt
Also, you can Use the composer package Paragonie/Certainty to manage CA certificates to avoid security vulnerabilities caused by the expiration of cacert.pem due to certificate revocation.
The above is the detailed content of Why Does My cURL HTTPS Connection Return Empty Content, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!