Home >Backend Development >PHP Tutorial >How to Enable Crypto When Using file_get_contents() with Specific HTTPS URLs?

How to Enable Crypto When Using file_get_contents() with Specific HTTPS URLs?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-23 12:17:30520browse

How to Enable Crypto When Using file_get_contents() with Specific HTTPS URLs?

Failed to Enable Crypto When Using file_get_contents() with Specific HTTPS URLs

When attempting to retrieve content from certain HTTPS URLs, such as https://eresearch.fidelity.com/, using file_get_contents(), you may encounter the following errors:

Warning: file_get_contents(): SSL: crypto enabling timeout
Warning: file_get_contents(): Failed to enable crypto
Warning: file_get_contents(): failed to open stream: operation failed
Fatal error: Maximum execution time of 30 seconds exceeded

Solution:

The issue arises from the target website utilizing the SSLv3 protocol, which is potentially unsupported by your PHP setup.

Option 1: Specify SSL Version Using cURL

Create a function using cURL:

<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); // Specify SSL version 3
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}</code>

Example usage:

<code class="php">var_dump(getSSLPage("https://eresearch.fidelity.com/eresearch/evaluate/analystsOpinionsReport.jhtml?symbols=api"));</code>

Option 2: Include Root Certificates in Windows (Optional)

In Windows, you may encounter a lack of access to root certificates. To resolve this, follow these steps:

  • Download root certificates from http://curl.haxx.se/docs/caextract.html
  • Modify your cURL code:
<code class="php">curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . "/certs/cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // True: verify certificates</code>

The above is the detailed content of How to Enable Crypto When Using file_get_contents() with Specific HTTPS URLs?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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