簡體   English   中英

帶有https的PHP file_get_contents參數返回false

[英]PHP file_get_contents parameter with https returns false

php.net中有關url的所有示例僅使用http演示,而未使用https演示。 我真的不明白為什么未通過https演示。 file_get_contents對於http URL返回true,對於https URL返回false。

  1. 我需要在正在執行此功能的服務器中啟用某些功能嗎?
  2. 我知道這是一個邏輯錯誤,但是此邏輯錯誤是在內置函數中發生的。 我沒有任何語法錯誤。 我在這里想念什么?
  3. 我的預期輸出是,當將https URL傳遞到file_get_contents時,從file_get_contents獲得true返回。

碼:

function connectMe() {
   $urlHeader = $_REQUEST['urlHeader'];
   // if this is a http url it's working for file_get_contents returns true
   // if this is a https url it's not working file_get_contents gives false
   $status = send($urlHeader);
   var_dump($status);
   echo $status ? 'success' : 'failed';
   exit;
}

function send($url) {
    $ctx = stream_context_create(
        array(
        'http'=>array(
        'header'=>"Content-type: application/x-www-form-urlencoded",
        'method'=>'POST',
        'content'=>NULL
        )
      )
    );
var_dump(stream_get_wrappers());
$w = stream_get_wrappers();
echo 'openssl: ',  extension_loaded  ('openssl') ? 'yes':'no', "\n";
echo 'http wrapper: ', in_array('http', $w) ? 'yes':'no', "\n";
echo 'https wrapper: ', in_array('https', $w) ? 'yes':'no', "\n";
echo 'wrappers: ', var_export($w);
return file_get_contents($url, 0, $ctx);
}

注意:1.上面的代碼已根據stackoverflow中給出的解決方案進行了集成,結果沒有任何變化。 2.啟用openssl並打開allow_url_fopen

我得到的輸出:

array(9){[0] =>字符串(5)“ https” [1] =>字符串(4)“ ftps” [2] =>字符串(3)“ php” [3] =>字符串(4) “文件” [4] =>字符串(4)“全局” [5] =>字符串(4)“數據” [6] =>字符串(4)“ http” [7] =>字符串(3)“ ftp “ [8] => string(4)” phar“}

openssl:是http包裝器:是https包裝器:是包裝器:數組(0 =>'https',1 =>'ftps',2 =>'php',3 =>'file',4 =>'glob', 5 =>'數據',6 =>'http',7 =>'ftp',8 =>'phar',)

警告:file_get_contents():php_network_getaddresses:gethostbyname失敗。 第40行的FILENAME.PHP中的errno = 0

警告:file_get_contents( https:// someIPhere或網站):無法打開流:php_network_getaddresses:gethostbyname失敗。 第40行的FILENAME.PHP中的errno = 0

bool(false)失敗

您可能會指向一個自簽名的ssl。 如果是這樣,那可能就是原因。 檢查以確保URL具有有效的SSL證書。

如果要繞過SSL驗證,則需要添加stream_context_create來設置'verify_peer'=> false。

請參閱: file_get_contents():SSL操作失敗,代碼為1(證書驗證失敗)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM