apache OPENSSL file_get_contents():无法启用加密

3z6pesqy  于 2023-05-01  发布在  Apache
关注(0)|答案(4)|浏览(122)

我正在建立一个个人股票平台(不分发)。我想拥有的一个组件是此页面上的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

我能看到的唯一区别是,fidelity页面在https标签附近有一个三角。

tag5nh1u

tag5nh1u1#

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

当您将SSL版本与curl设置为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,否则你会得到一个错误。

brqmpdu1

brqmpdu12#

有同样的问题-它在ca证书的某个地方,所以我使用了用于curl的ca捆绑包,它工作了。你可以在这里下载curl ca bundle: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);

希望能帮上忙

2ekbmq32

2ekbmq323#

一个适合我的解决方法是禁用对等验证。使用时要小心,因为它不应该用于生产。

$arrContextOptions = array(
      "ssl" => array(
        "verify_peer" => false,
        "verify_peer_name" => false,
      )
);  
  
$context = stream_context_create($arrContextOptions);
$contents = file_get_contents($url,false,$context);
klh5stk1

klh5stk14#

看看是哪个phpini文件当前由php-fpm服务使用。设置curl_cainfo和openssl。cafile变量使用相同的和vaid ca文件。这将解决我的问题。

相关问题