ホームページ  >  記事  >  バックエンド開発  >  特定の HTTPS URL で file_get_contents() を使用するときに暗号化を有効にする方法

特定の HTTPS URL で file_get_contents() を使用するときに暗号化を有効にする方法

Mary-Kate Olsen
Mary-Kate Olsenオリジナル
2024-10-23 12:17:30393ブラウズ

How to Enable Crypto When Using file_get_contents() with Specific HTTPS URLs?

特定の HTTPS URL で file_get_contents() を使用するときに暗号化を有効にできませんでした

https などの特定の HTTPS URL からコンテンツを取得しようとしたとき://eresearch.fidelity.com/ で file_get_contents() を使用すると、次のエラーが発生する可能性があります:

Warning: file_get_contents(): SSL: crypto enabling timeout
Warning: file_get_contents(): Failed to enable crypto
Warning: file_get_contents(): failed to open stream: operation failed
Fatal error: Maximum execution time of 30 seconds exceeded

解決策:

この問題は、 SSLv3 プロトコルを使用するターゲット Web サイトは、PHP セットアップでサポートされていない可能性があります。

オプション 1: cURL を使用して SSL バージョンを指定する

cURL を使用して関数を作成します:

<code class="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); // Specify SSL version 3
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}</code>

使用例:

<code class="php">var_dump(getSSLPage("https://eresearch.fidelity.com/eresearch/evaluate/analystsOpinionsReport.jhtml?symbols=api"));</code>

オプション 2: Windows にルート証明書を含める (オプション)

Windows では、ルート証明書が不足している場合があります。ルート証明書へのアクセスの制限。これを解決するには、次の手順に従います:

  • http://curl.haxx.se/docs/caextract.html からルート証明書をダウンロードします
  • cURL コードを変更します:
<code class="php">curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . "/certs/cacert.pem");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // True: verify certificates</code>

以上が特定の HTTPS URL で file_get_contents() を使用するときに暗号化を有効にする方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。