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