如何让 PHP 生成分块响应 [英] How to make PHP generate Chunked response

查看:19
本文介绍了如何让 PHP 生成分块响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用谷歌搜索了这个问题,但没有答案.

I googled for this problem but there is no answer for it.

我希望我的 PHP 脚本生成分块的 HTTP 响应(http://en.wikipedia.org/wiki/Chunked_transfer_encoding).怎么做?

I want my PHP script to generate HTTP response in chunked( http://en.wikipedia.org/wiki/Chunked_transfer_encoding). How to do it?

更新:我想到了.我必须指定 Transfer-encoding 标头并刷新它.

Update: I figured it out. I have to specify Transfer-encoding header and flush it.

header("Transfer-encoding: chunked");
flush(); 

冲洗是必要的.否则,将生成 Content-Length 标头.

The flush is necessary. Otherwise, Content-Length header will be generated.

而且,我必须自己做块.有辅助函数,不难.

And, I have to make chunks by myself. With a helper function, it is not hard.

function dump_chunk($chunk)
{
    echo sprintf("%x
", strlen($chunk));
    echo $chunk;
    echo "
";
}

推荐答案

如果您不指定内容长度标头,PHP 响应将始终被分块,并且会发生刷新.(这会在 x 字节后自动发生,只是不知道确切多少).

A PHP response will always be chunked if you don't specify a content-length header, and a flush occurs. (which will happen automatically after x bytes, just don't know exactly how much).

这是一个奇怪的事情要关心.这是某种学术/学习练习,还是您要解决的现实问题?

This is a weird thing to care about. Is this some kind of academic/learning exercise or is there a real world problem you're trying to solve?

这篇关于如何让 PHP 生成分块响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆