(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
stream_get_meta_data — 检索流/文件指针的头部/元数据
结果数组包含以下项目
timed_out (布尔值) - 如果流在上次调用 fread() 或 fgets() 时等待数据超时,则为 true。
blocked (布尔值) - 如果流处于阻塞 IO 模式,则为 true。请参阅 stream_set_blocking()。
eof (布尔值) - 如果流已到达文件结尾,则为 true。请注意,对于套接字流,即使 unread_bytes 不为零,此成员也可能为 true。要确定是否有更多数据要读取,请使用 feof() 而不是读取此项。
unread_bytes (整数) - 当前包含在 PHP 自己的内部缓冲区中的字节数。
注意: 您不应该在脚本中使用此值。
stream_type (字符串) - 描述流底层实现的标签。
wrapper_type (字符串) - 描述覆盖在流上的协议封装器实现的标签。有关封装器的更多信息,请参阅 支持的协议和封装器。
wrapper_data (混合类型) - 附加到此流的特定于封装器的数据。有关封装器及其封装器数据的更多信息,请参阅 支持的协议和封装器。
mode (字符串) - 此流所需的访问类型(请参阅 fopen() 参考中的表 1)
seekable (布尔值) - 当前流是否可以搜索。
uri (字符串) - 与此流关联的 URI/文件名。
crypto (数组) - 此流的 TLS 连接元数据。(注意:仅在资源的流使用 TLS 时提供。)
示例 #1 使用 fopen() 和 http 的 stream_get_meta_data() 示例
<?php
$url = 'http://www.example.com/';
if (!$fp = fopen($url, 'r')) {
trigger_error("无法打开 URL ($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 扩展 创建的套接字。