PHP HTTP 上下文選項


引言

以下是http://https://傳輸的上下文選項列表

method遠端伺服器支援的 HTTP 方法。預設值為 GET。
header在請求期間傳送其他標頭。
user_agent傳送 User-Agent: 標頭時的值。預設情況下,使用 user_agent php.ini 設定。
content在標頭之後傳送的其他資料。通常與 POST 或 PUT 請求一起使用。
proxy指定代理伺服器地址的 URI。
request_fulluri 布林值如果設定為 TRUE,則在構建請求時將使用整個 URI。預設值為 FALSE。
follow_location關注 Location 標頭重定向。設定為 0 表示停用。預設為 1。
max_redirects要關注的最大重定向數。
protocol_versionHTTP 協議版本。預設為 1.0。
timeout讀取超時(以秒為單位),由一個浮點型指定(例如 10.5)。
ignore_errors即使在錯誤狀態程式碼下獲取內容。預設為 FALSE。

以下示例從http:// URL 獲取標頭檔案和內容

示例

<?php
$url = "https:///testscript.php";
$opts = array('http' =>
array(
   'method' => 'GET',
   'max_redirects' => '0',
   'ignore_errors' => '1'
);
$context = stream_context_create($opts);
$stream = fopen($url, 'r', false, $context);
var_dump(stream_get_meta_data($stream));
?>

輸出

它顯示標題資訊和元資料如下 −

array(10) {
   ["timed_out"]=>
   bool(false)
   ["blocked"]=>
   bool(true)
   ["eof"]=>
   bool(false)
   ["wrapper_data"]=>
   array(7) {
      [0]=>
      string(15) "HTTP/1.1 200 OK"
      [1]=>
      string(35) "Date: Thu, 17 Sep 2020 07:04:47 GMT"
      [2]=>
      string(55) "Server: Apache/2.4.41 (Win64) OpenSSL/1.0.2s PHP/7.1.32"
      [3]=>
      string(24) "X-Powered-By: PHP/7.1.32"
      [4]=>
      string(17) "Content-Length: 0"
      [5]=>
      string(17) "Connection: close"
      [6]=>
      string(38) "Content-Type: text/html; charset=UTF-8"
   }
["wrapper_type"]=>
string(4) "http"
["stream_type"]=>
string(14) "tcp_socket/ssl"
["mode"]=>
string(1) "r"
["unread_bytes"]=>
int(0)
["seekable"]=>
bool(false)
["uri"]=>
string(31) "https:///testscript.php"
}

更新於: 21-Sep-2020

401 次瀏覽

開啟你的職業之路

完成課程獲得認證

開始學習
廣告
© . All rights reserved.