2024 年 PHP Conference Japan

stream_get_meta_data

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

stream_get_meta_data從串流/檔案指標擷取標頭/中繼資料

說明

stream_get_meta_data(資源 $stream): 陣列

返回有關現有 stream 的資訊。

參數

stream

此資料流可以是任何由 fopen()fsockopen()pfsockopen()stream_socket_client() 建立的資料流。

返回值

結果陣列包含以下項目

  • timed_out (bool) - 若資料流在上次呼叫 fread()fgets() 時等待資料逾時,則為 true

  • blocked (bool) - 若資料流處於阻斷式 IO 模式,則為 true。詳見 stream_set_blocking()

  • eof (bool) - 若資料流已到達檔案結尾,則為 true。請注意,對於 Socket 資料流,即使 unread_bytes 不為零,此成員也可能為 true。要判斷是否還有更多資料要讀取,請使用 feof() 而不是讀取此項目。

  • unread_bytes (int) - 目前包含在 PHP 自身內部緩衝區中的位元組數。

    注意您不應在腳本中使用此值。

  • stream_type (string) - 描述資料流底層實作的標籤。

  • wrapper_type (string) - 描述覆蓋在資料流上的協定包裝器實作的標籤。有關包裝器的更多資訊,請參閱 支援的協定和包裝器

  • wrapper_data (mixed) - 附加到此資料流的包裝器特定資料。有關包裝器及其包裝器資料的更多資訊,請參閱 支援的協定和包裝器

  • mode (string) - 此資料流所需的存取類型(請參閱 fopen() 參考文件的表 1)

  • seekable (bool) - 目前資料流是否可以搜尋。

  • uri (string) - 與此資料流關聯的 URI/檔案名稱。

  • crypto (array) - 此資料流的 TLS 連線中繼資料。(注意:僅在資源的資料流使用 TLS 時提供。)

範例

範例 #1 使用 fopen() 搭配 http 的 stream_get_meta_data() 範例

<?php
$url
= 'http://www.example.com/';

if (!
$fp = fopen($url, 'r')) {
trigger_error("無法開啟網址 ($url)", E_USER_ERROR);
}

$meta = stream_get_meta_data($fp);

var_dump($meta);

fclose($fp);
?>

上述範例將輸出類似以下的內容

array(10) {
  'timed_out' =>
  bool(false)
  'blocked' =>
  bool(true)
  'eof' =>
  bool(false)
  'wrapper_data' =>
  array(13) {
    [0] =>
    string(15) "HTTP/1.1 200 OK"
    [1] =>
    string(11) "Age: 244629"
    [2] =>
    string(29) "Cache-Control: max-age=604800"
    [3] =>
    string(38) "Content-Type: text/html; charset=UTF-8"
    [4] =>
    string(35) "Date: Sat, 20 Nov 2021 18:17:57 GMT"
    [5] =>
    string(24) "Etag: "3147526947+ident""
    [6] =>
    string(38) "Expires: Sat, 27 Nov 2021 18:17:57 GMT"
    [7] =>
    string(44) "Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT"
    [8] =>
    string(22) "Server: ECS (chb/0286)"
    [9] =>
    string(21) "Vary: Accept-Encoding"
    [10] =>
    string(12) "X-Cache: HIT"
    [11] =>
    string(20) "Content-Length: 1256"
    [12] =>
    string(17) "Connection: close"
  }
  'wrapper_type' =>
  string(4) "http"
  'stream_type' =>
  string(14) "tcp_socket/ssl"
  'mode' =>
  string(1) "r"
  'unread_bytes' =>
  int(1256)
  'seekable' =>
  bool(false)
  'uri' =>
  string(23) "http://www.example.com/"
}

範例 #2 使用 stream_socket_client() 搭配 https 的 stream_get_meta_data() 範例

<?php
$streamContext
= stream_context_create(
[
'ssl' => [
'capture_peer_cert' => true,
'capture_peer_cert_chain' => true,
'disable_compression' => true,
],
]
);

$client = stream_socket_client(
'ssl://www.example.com:443',
$errorNumber,
$errorDescription,
40,
STREAM_CLIENT_CONNECT,
$streamContext
);


$meta = stream_get_meta_data($client);

var_dump($meta);
?>

上述範例將輸出類似以下的內容

array(8) {
  'crypto' =>
  array(4) {
    'protocol' =>
    string(7) "TLSv1.3"
    'cipher_name' =>
    string(22) "TLS_AES_256_GCM_SHA384"
    'cipher_bits' =>
    int(256)
    'cipher_version' =>
    string(7) "TLSv1.3"
  }
  'timed_out' =>
  bool(false)
  'blocked' =>
  bool(true)
  'eof' =>
  bool(false)
  'stream_type' =>
  string(14) "tcp_socket/ssl"
  'mode' =>
  string(2) "r+"
  'unread_bytes' =>
  int(0)
  'seekable' =>
  bool(false)
}

注意事項

注意:

此函式不適用於由 Socket 擴充套件 建立的通訊端。

參見

新增註釋

使用者提供的註釋

此頁面沒有使用者提供的註釋。
To Top