PHP:如何从块下载的流请求中获取内容长度

问题描述 投票:0回答:1

我正在尝试分块下载外部站点档案,但这种方法的问题是,当我尝试通过

Content-Length
获取
get_headers()
时,它不存在。

有人在保存响应块并获取内容长度时发现了这个问题

$options = array(
    'http' => array(
        'header' => "Authorization: Bearer $accessToken\r\n"
    )
);
$context = stream_context_create($options);
    
// Get the Content-Length to limit the file download above 1GB.
// $file_headers = get_headers($sourceFile, 1, $context);
// if($file_headers["Content-Type"])

$sourceFileStream = fopen($sourceFile, 'rb', false, $context);

如何获取流中响应的 Content-Type 或大小

php http stream
1个回答
0
投票

我假设响应是通过

Transfer-Encoding: chunked

发送的

因此,没有

Content-Length
标头字段(RFC 7320): 发送方不得在任何包含 Transfer-Encoding 标头字段的消息中发送 Content-Length 标头字段。

但你很幸运 - RFC 还提供了一种 算法来解码分块传输并获取其长度。

© www.soinside.com 2019 - 2024. All rights reserved.