首頁  >  文章  >  後端開發  >  如何在使用具有特定 HTTPS URL 的 file_get_contents() 時啟用加密?

如何在使用具有特定 HTTPS URL 的 file_get_contents() 時啟用加密?

Mary-Kate Olsen
Mary-Kate Olsen原創
2024-10-23 12:17:30389瀏覽

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

在特定HTTPS URL 中使用file_get_contents() 時無法啟用加密

嘗試從某些HTTPS URL(例如https)擷取內容時://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 協定的目標網站,您的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中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn