When encountering the error "Failed to enable crypto" while using file_get_contents() with OPENSSL, it's crucial to investigate the underlying issue.
Identifying the Root Cause
The provided error log suggests a timeout during crypto initialization. However, the problem may lie elsewhere. One possible cause is that the website uses an unsupported SSL version, such as SSLv3.
Solution Using cURL
To bypass the limitations of file_get_contents() and enable SSLv3 support, it's recommended to use cURL instead:
<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>
This solution explicitly sets the SSL version to v3, allowing curl to handle the SSL handshake successfully.
Additional Considerations for Windows Users
For Windows users, it may also be necessary to specify the location of root certificates for curl. This can be achieved by:
<code class="php">curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . "/certs/cacert.pem"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);</code>
By setting the CURLOPT_SSL_VERIFYPEER option to true, curl will verify the peer's certificate against the specified root certificates.
The above is the detailed content of How to Resolve Failed Crypto Initialization for file_get_contents() Using OpenSSL?. For more information, please follow other related articles on the PHP Chinese website!