Failed "file_get_contents()": Resolving "Failed to enable crypto" for HTTPS Requests
Problem:
When attempting to fetch an HTTPS webpage using file_get_contents(), some users encounter the error "Failed to enable crypto." This issue particularly affects the URL "https://eresearch.fidelity.com/eresearch/evaluate/fundamentals/earnings.jhtml?stockspage=earnings&symbols=AAPL&showPriceLine=yes."
Cause:
The error stems from the use of SSLv3 by the affected website. The openssl module in PHP has known compatibility issues with older SSL versions.
Resolution:
To resolve the issue, modify the file_get_contents() code to utilize the cURL extension, which allows for specifying the SSL version. The following code snippet demonstrates this solution:
<code class="php">function getSSLPage($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSLVERSION,3); $result = curl_exec($ch); curl_close($ch); return $result; } var_dump(getSSLPage("https://eresearch.fidelity.com/eresearch/evaluate/analystsOpinionsReport.jhtml?symbols=api"));</code>
Alternative Resolution for Windows Users:
On Windows systems, there may be an additional challenge due to lack of access to root certificates. To resolve this:
<code class="php">curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . "/certs/cacert.pem"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);</code>
Note: Make sure to enable SSL verification (CURLOPT_SSL_VERIFYPEER) or you will encounter errors.
The above is the detailed content of How to Resolve \'Failed to enable crypto\' Error for HTTPS Requests in PHP?. For more information, please follow other related articles on the PHP Chinese website!