PHP 中 stream_get_meta_data() 的返回值格式



我正在编写一个简单的PHP脚本,但我发现了一个奇怪的行为(不是错误):stream_get_meta_data()的返回值格式在不同的平台上不一致。

下面列出了简化的示例代码:

<?php
    $handle = fopen("http://www.example.com", "r");
    if ($handle) {
        $dump = fgets($handle);
        $stream_meta = stream_get_meta_data( $handle );
        echo print_r( $stream_meta, true );
        fclose($handle);
    }
?>

这个PHP脚本应该在我的QNAP TS-269 NAS上运行。像其他人一样,我在PC上编写和测试代码,然后转移到NAS。

当我在PC上执行此代码时,我得到以下结果:

Array
(
    [timed_out] => 
    [blocked] => 1
    [eof] => 
    [wrapper_data] => Array
        (
            [0] => HTTP/1.0 200 OK
            [1] => Cache-Control: max-age=604800
            [2] => Content-Type: text/html
            [3] => Date: Mon, 30 Jan 2017 08:44:14 GMT
            [4] => Etag: "359670651+ident"
            [5] => Expires: Mon, 06 Feb 2017 08:44:14 GMT
            [6] => Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
            [7] => Server: ECS (rhv/818F)
            [8] => Vary: Accept-Encoding
            [9] => X-Cache: HIT
            [10] => x-ec-custom-error: 1
            [11] => Content-Length: 1270
            [12] => Connection: close
        )
    [wrapper_type] => http
    [stream_type] => tcp_socket/ssl
    [mode] => r
    [unread_bytes] => 1254
    [seekable] => 
    [uri] => http://www.example.com
)

但在 NAS 上,结果变为:

Array
(
    [wrapper_data] => Array
        (
            [headers] => Array
                (
                    [0] => HTTP/1.1 200 OK
                    [1] => Cache-Control: max-age=604800
                    [2] => Content-Type: text/html
                    [3] => Date: Mon, 30 Jan 2017 08:44:25 GMT
                    [4] => Etag: "359670651+ident"
                    [5] => Expires: Mon, 06 Feb 2017 08:44:25 GMT
                    [6] => Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
                    [7] => Server: ECS (rhv/818F)
                    [8] => Vary: Accept-Encoding
                    [9] => X-Cache: HIT
                    [10] => x-ec-custom-error: 1
                    [11] => Content-Length: 1270
                )
            [readbuf] => Resource id #6
        )
    [wrapper_type] => cURL
    [stream_type] => cURL
    [mode] => r
    [unread_bytes] => 1254
    [seekable] => 
    [uri] => http://www.example.com
    [timed_out] => 
    [blocked] => 1
    [eof] => 
)

$result['wrapper_data']的数组结构是不同的。第一个结果(在PC上)与PHP官方网站上的示例匹配。在 NAS 上,这些 HTTP 响应标头收集在额外的级别 $result['wrapper_data']['header'] 下。

PHP 版本在 PC 上为 5.6.28,在 NAS 上为 5.3.29。

这是正常行为吗?由于不同的PHP版本或其他什么?

好吧,您似乎有两个不同的包装器("wrapper_type"字段):PC上的http和NAS上的cURL。也许您在一种情况下将脚本作为 Apache 模块运行,而在另一种情况下以 CLI 模式运行?

上级:http://php.net/manual/en/curl.installation.php"从 PHP 4.3.0 开始,您可以将 PHP 配置为使用 cURL 用于 URL 流 - with-curlwrappers。此功能已从 PHP 5.5.0 移至 PECL"

最新更新