php OPENSSL file_get_contents():无法启用加密
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14078182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OPENSSL file_get_contents(): Failed to enable crypto
提问by John Smith
I'm building a personal stock platform (not distributed). A component I would like to have is the EPS graph on this page:
我正在构建一个个人股票平台(非分布式)。我想要的一个组件是此页面上的 EPS 图:
As you can see, the page is https, so after days of hammering things out, I enabled openssland now it seems to work for all httpspages such as the homepages of facebook and twitter, however it is still not working for the one I need.
正如您所看到的,页面是https,所以经过几天的锤炼,我启用openssl了它,现在它似乎适用于所有https页面,例如 facebook 和 twitter 的主页,但是它仍然不适用于我需要的页面。
file_get_contents('https://facebook.com'); /* works */
file_get_contents('https://twittercom'); /* works */
file_get_contents('https://eresearch.fidelity.com/eresearch/evaluate/fundamentals/earnings.jhtml?stockspage=earnings&symbols=AAPL&showPriceLine=yes');
I'm getting the warning:
我收到警告:
Warning: file_get_contents(): SSL: crypto enabling timeout in C:\xampp\htdocs\index.php on line 3
Warning: file_get_contents(): Failed to enable crypto in C:\xampp\htdocs\index.php on line 3
Warning: file_get_contents(https://eresearch.fidelity.com/eresearch/evaluate/fundamentals/earnings.jhtml?stockspage=earnings&symbols=AAPL&showPriceLine=yes): failed to open stream: operation failed in C:\xampp\htdocs\index.php on line 3
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\index.php on line 3
The only difference I can see is that the fidelity page has a triangle near the https label.
我能看到的唯一区别是保真页面在 https 标签附近有一个三角形。


回答by René H?hle
Ok I have found a solution. The problem is that the site uses SSLv3. And I know that there are some problems in the openssl module. Some time ago I had the same problem with the SSL versions.
好的,我找到了解决方案。问题是该站点使用 SSLv3。而且我知道openssl模块存在一些问题。前段时间我在 SSL 版本上遇到了同样的问题。
<?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"));
?>
When you set the SSL Version with curl to v3 then it works.
当您使用 curl 将 SSL 版本设置为 v3 时,它就可以工作了。
Edit:
编辑:
Another problem under Windows is that you don't have access to the certificates. So put the root certificates directly to curl.
Windows 下的另一个问题是您无权访问证书。所以把根证书直接curl。
http://curl.haxx.se/docs/caextract.html
http://curl.haxx.se/docs/caextract.html
here you can download the root certificates.
在这里您可以下载根证书。
curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . "/certs/cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
Then you can use the CURLOPT_SSL_VERIFYPEERoption with trueotherwise you get an error.
然后您可以使用该CURLOPT_SSL_VERIFYPEER选项,true否则您会收到错误消息。
回答by user7457877
Had same problem - it was somewhere in the ca certificate, so I used the ca bundle used for curl, and it worked. You can download the curl ca bundle here: https://curl.haxx.se/docs/caextract.html
有同样的问题 - 它在 ca 证书的某个地方,所以我使用了用于 curl 的 ca 包,并且它起作用了。你可以在这里下载 curl ca 包:https: //curl.haxx.se/docs/caextract.html
For encryption and security issues see this helpful article:
https://www.venditan.com/labs/2014/06/26/ssl-and-php-streams-part-1-you-are-doing-it-wrongtm/432
有关加密和安全问题,请参阅这篇有用的文章:https:
//www.venditan.com/labs/2014/06/26/ssl-and-php-streams-part-1-you-are-doing-it-wrongtm/ 432
Here is the example:
这是示例:
$url = 'https://www.example.com/api/list';
$cn_match = 'www.example.com';
$data = array (
'apikey' => '[example api key here]',
'limit' => intval($limit),
'offset' => intval($offset)
);
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
, 'ssl' => array(
'verify_peer' => true,
'cafile' => [path to file] . "cacert.pem",
'ciphers' => 'HIGH:TLSv1.2:TLSv1.1:TLSv1.0:!SSLv3:!SSLv2',
'CN_match' => $cn_match,
'disable_compression' => true,
)
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
Hope that helps
希望有帮助

