[英]PHP GZip and Server Sent Event Streams
I'm writing a server sent event stream in PHP and I receive the error: 我正在用PHP写一个服务器发送的事件流,我收到错误:
"failed to flush buffer zlib output compression" “无法刷新缓冲区zlib输出压缩”
This I believe is due to trying to flush the gzipped output. 我相信这是因为尝试刷新gzip压缩输出。
Here's my PHP code: 这是我的PHP代码:
header ("Content-Type: text/event-stream\n\n");
header ("Cache-Control: no-cache");
echo "data: {$json}";
echo "\n\n";
ob_flush(); // ERROR HERE
flush();
My question is what is the best way to get this working - ideally without disabling gzip in apache - can it be turned off in PHP? 我的问题是什么是最好的方法来实现这一点 - 理想情况下不会在apache中禁用gzip - 它可以在PHP中关闭吗?
I tried this but it didn't work: 我尝试了这个,但它不起作用:
if(ini_get('zlib.output_compression')){
ini_set('zlib.output_compression', 'Off');
}
You cannot use zlib output compression along ob_ output handler. 您不能沿ob_ output处理程序使用zlib输出压缩。 See the php docs on zlib.output_compression, it states it multiple times. 请参阅zlib.output_compression上的php文档,它会多次声明。
If found out that the easiest way to enable output compression in php is to just do this: 如果发现在php中启用输出压缩的最简单方法就是这样做:
ini_set("zlib.output_compression", 1);
ini_set("zlib.output_compression_level", 9);
And loose all the ob_* stuff. 并且松开了所有的ob_ *东西。 Now when a client requests a page with a header like : 现在,当客户端请求带有标题的页面时:
Accept-Encoding: gzip, deflate
zlib will gzip your response body for you AND it will set this for you in the response: zlib会为你解压缩你的响应主体,它会在响应中为你设置这个:
Content-Encoding: gzip
I spent hours of my life on this before realising how simple it is with those 2 lines. 我花了几个小时的时间来实现这一点,然后才意识到这两条线是多么简单。 and don't use any flush at all implicitely 并且根本不要使用任何冲洗
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.