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

Mary-Kate Olsen
發布: 2024-10-23 12:17:30
原創
388 人瀏覽過

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中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!