OPENSSL file_get_contents():无法启用encryption

我正在build立一个个人股票平台(不是分布式的)。 我想要的一个组件是本页面的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标签附近有一个三angular形。

保真度https标签

好的,我find了一个解决scheme。 问题是该网站使用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")); ?> 

当你设置curlv3的SSL版本,然后它的工作。

编辑:

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选项,否则你会得到一个错误。

有同样的问题 – 它是在证书的某个地方,所以我使用的curl用于curl,它的工作。 您可以在这里下载curl ca包: https : //curl.haxx.se/docs/caextract.html

有关encryption和安全问题,请参阅这篇有用的文章
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); 

希望有所帮助

另外一个很好的方法,至less在debugging时,像下面这样是wget和exec()函数。

 exec('wget -O /local/destination/folder/filename.tar.gz www.domain.com/files/filename.tar.gz --no-check-certificate');

“–no-check-certificate”标志可以处理任何有问题的证书。 在生产环境中谨慎使用此方法。