How to Resolve \'Failed to enable crypto\' Error for HTTPS Requests in PHP?

Patricia Arquette
Release: 2024-10-23 19:00:02
Original
886 people have browsed it

How to Resolve

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>
Copy after login

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:

  1. Download the root certificates from http://curl.haxx.se/docs/caextract.html.
  2. Specify the path to the downloaded "cacert.pem" file using the following cURL options:
<code class="php">curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . "/certs/cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);</code>
Copy after login

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!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!