web-dev-qa-db-ja.com

OPENSSL file_get_contents():暗号化を有効にできませんでした

私は個人的な株式プラットフォームを構築しています(配布されていません)。私が持ちたいコンポーネントは、このページのEPSグラフです。

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

ご覧のとおり、ページはhttpsであるため、何日もハンマーを使い続けた後、opensslを有効にしました。現在、ホームページなどのすべてのhttpsページで機能しているようですフェイスブックとツイッターの、しかしそれはまだ私が必要とするもののために働いていません。

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ラベルの近くに三角形があることです。

fidelity https label

36
John Smith

解決策を見つけました。問題は、サイトが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でのもう1つの問題は、証明書にアクセスできないことです。したがって、ルート証明書を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を指定すると、エラーが発生します。

51
René Höhle

同じ問題がありました-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);

役立つことを願っています

5
user7457877