【问题标题】:OPENSSL file_get_contents(): Failed to enable cryptoOPENSSL file_get_contents():无法启用加密
【发布时间】:2012-12-14 05:32:16
【问题描述】:

我正在建立一个个人股票平台(未分发)。我想要的一个组件是此页面上的 EPS 图:

https://eresearch.fidelity.com/eresearch/evaluate/fundamentals/earnings.jhtml?stockspage=earnings&symbols=AAPL&showPriceLine=yes

如您所见,该页面是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');

我收到警告:

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

我能看到的唯一区别是保真度页面在 https 标签附近有一个三角形。

【问题讨论】:

  • @Stony 我已经测试了所有的 stackoverflow 答案。对于您提供的链接,第一个答案是:openssl: yes http wrapper: yes https wrapper: yes 从 phpinfo() 打开或启用所有 SSL 选项。第二个答案:extension=php_openssl.dllallow_url_include = On在php.ini中启用。
  • 非常有趣,我试过了,但 30 秒后我超时了。目标站点有问题。也许你尝试一个 curl 请求。
  • 嗯...那我得弄清楚如何配置 CURL。
  • OK 与 CURL 相同的问题。也许创建该站点的人已经更改了您无法从该站点获取数据的内容。也许它的目的。也许该网站有 API?

标签: php apache openssl


【解决方案1】:

好的,我找到了解决方案。问题是该站点使用 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"));
?>

当您使用 curl 将 SSL 版本设置为 v3 时,它可以工作。

编辑:

Windows 下的另一个问题是您无权访问证书。所以直接把根证书放到curl里面。

http://curl.haxx.se/docs/caextract.html

您可以在此处下载根证书。

curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . "/certs/cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

然后您可以将CURLOPT_SSL_VERIFYPEER 选项与true 一起使用,否则会出现错误。

【讨论】:

  • 是的,问题是file_get_contents 方法使用旧的 SSL 版本。有一些 Bug 报告,但直到现在还没有解决方案。
  • @Stony 你有没有提到哪个 PHP 错误报告适用于此?
  • 没有curl有解决办法吗?
  • 我不认为这是一个 PHP 错误(不是 PHP 有旧的 SSL 版本) - 这是您连接的站点使用旧 SSL。 SSLv3 自 1999 年以来实际上已经过时了! This pull request 添加了对使用哪个加密库的控制,但看起来它只会出现在 PHP 5.5 的某些未来版本中。同时,curl 确实解决了这个问题,或者您可以使用 pecl_http 扩展实现相同的目的。
  • 只有在知道自己在做什么的情况下才将 CURLOPT_SSL_VERIFYPEER 设置为 FALSE。
【解决方案2】:

有同样的问题 - 它在 ca 证书中的某个地方,所以我使用了用于 curl 的 ca 包,并且它有效。你可以在这里下载 curl ca 包:https://curl.haxx.se/docs/caextract.html

有关加密和安全问题,请参阅这篇有用的文章:
https://www.venditan.com/labs/2014/06/26/ssl-and-php-streams-part-1-you-are-doing-it-wrongtm/432

示例如下:

    $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);

希望有帮助

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-11-26
  • 2019-08-15
  • 1970-01-01
  • 2012-12-04
  • 1970-01-01
  • 1970-01-01
  • 2014-11-05
相关资源
最近更新 更多