如何在 PHP 中禁用输出缓冲

发布于 2024-12-27 15:32:20 字数 1092 浏览 3 评论 0原文

我编写了一个简单的中继脚本,它连接到网络摄像头并从套接字读取数据,并使用打印功能输出此数据。数据是已设置边界的 MJPG 数据。我只是输出读取到的数据。

问题是 PHP 似乎正在缓冲这些数据。当我将相机设置为 1 FPS 时,画面会冻结 7-8 秒,然后快速显示 8 帧。如果我将分辨率设置为很大,相机会以每秒或多或少 1 帧的速度移动。我假设正在发生一些缓冲(因为大尺寸会快速填充缓冲区,而小尺寸则不会),并且我不知道如何禁用此缓冲。有人知道怎么做吗?

代码:

ignore_user_abort(false);

$boundary = "myboundary";

//Set this so PHP doesn't timeout during a long stream
set_time_limit(0);

$socketConn = @fsockopen ("192.168.1.6", 1989, $errno, $errstr, 2);
if (!$socketConn)
exit();
stream_set_timeout($socketConn, 10);
fputs ($socketConn, "GET /mjpeg HTTP/1.0\r\n\r\n");

//Setup Header Information
header("Cache-Control: no-cache");
header("Cache-Control: private");
header("Pragma: no-cache");
header("Content-type: multipart/x-mixed-replace; boundary=$boundary");

@ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++)
ob_end_flush();
ob_implicit_flush(1);

stream_set_blocking($f2, false);

//Send data to client
while (connection_status() == CONNECTION_NORMAL)
{
    $chunk = fread($socketConn, 128);
    print $chunk;   
}

fclose($socketConn);

I wrote a simple relay script that connects to a web camera and reads from the socket, and outputs this data using the print function. The data is MJPG data with boundaries already setup. I just output the data that is read.

The problem is PHP seems to be buffering this data. When I set the camera to 1 FPS, the feed will freeze for 7-8 seconds, then quickly display 8 frames. If I set the resolution to a huge size, the camera move at more or less 1 frame per second. I assume then some buffering is happening (since huge sizes fill the buffer quickly, and low sizes don't), and I can't figure out how to disable this buffering. Does anyone know how to?

Code:

ignore_user_abort(false);

$boundary = "myboundary";

//Set this so PHP doesn't timeout during a long stream
set_time_limit(0);

$socketConn = @fsockopen ("192.168.1.6", 1989, $errno, $errstr, 2);
if (!$socketConn)
exit();
stream_set_timeout($socketConn, 10);
fputs ($socketConn, "GET /mjpeg HTTP/1.0\r\n\r\n");

//Setup Header Information
header("Cache-Control: no-cache");
header("Cache-Control: private");
header("Pragma: no-cache");
header("Content-type: multipart/x-mixed-replace; boundary=$boundary");

@ini_set('implicit_flush', 1);
for ($i = 0; $i < ob_get_level(); $i++)
ob_end_flush();
ob_implicit_flush(1);

stream_set_blocking($f2, false);

//Send data to client
while (connection_status() == CONNECTION_NORMAL)
{
    $chunk = fread($socketConn, 128);
    print $chunk;   
}

fclose($socketConn);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

泅人 2025-01-03 15:32:20

tl;dr version

做两件事:

  1. 禁用用户空间输出缓冲区,或者...

    • 在全球范围内,无论是...

      • 关闭 php.ini 中的 output_buffering,或者

      • 使用以下命令关闭 Apache 配置中的 output_buffering

         php_flag“output_buffering”关闭
        
    • 或者只是您关心的脚本,通过...

      • 调用ob_end_flush(),或
      • 调用ob_end_clean()
  2. 此外,禁用服务器级输出缓冲区尽可能多地通过以下任一方式:

    • 在脚本开头调用 ob_implicit_flush(),或者
    • 在每个 echo 语句或其他将输出添加到响应正文的语句之后调用 flush()

较长版本

令人困惑的是,有两层可能相关的缓冲,并且 PHP 文档在区分这两层方面做得很差。

输出缓冲区

第一层通常被 PHP 文档称为“输出缓冲区”。这一层缓冲仅影响 HTTP 响应正文的输出,而不影响标头。您可以使用 ob_start(),然后使用 ob_end_flush()ob_end_clean()。您还可以使用 output_buffering 选项。

php.ini 的生产版本<的此选项的默认值/a> 是 4096,这意味着输出的前 4096 字节将被缓冲在输出缓冲区中,此时它将被刷新并关闭输出缓冲。

使用,如果您使用的是 Apache)来全局禁用这一层缓冲

php_flag "output_buffering" Off

您可以通过在 php.ini 文件中将 output_buffering 设置为 Off(或者在 Apache 配置中 。或者,您可以通过在脚本开头调用 ob_end_clean()ob_end_flush() 来禁用单个脚本。

写入缓冲区和 Web 服务器缓冲区

除了输出缓冲区之外,PHP 手册中还提到了“写入缓冲区”,以及 Web 服务器具有的任何缓冲系统。如果您通过 mod_php 将 PHP 与 Apache 结合使用,并且不使用 mod_gzip,则可以调用 flush() 刷新这些;对于其他后端,它也可能有效,尽管手册对于给出保证很谨慎:

描述

无效冲洗 (无效)

刷新 PHP 的写入缓冲区以及 PHP 使用的任何后端(CGI、Web 服务器等)。这试图将当前输出一直推送到浏览器,但有一些警告。

flush() 可能无法覆盖 Web 服务器的缓冲方案,并且它对浏览器中的任何客户端缓冲没有影响。它也不会影响 PHP 的用户空间输出缓冲机制。这意味着您必须同时调用 ob_flush()flush() 来刷新 ob 输出缓冲区(如果您正在使用它们)。

还有几种方法可以让 PHP 在每次 echo 任何内容(或执行任何其他将输出回显到响应正文的操作)时自动调用 flush()

第一种是调用 ob_implicit_flush()。请注意,该函数的名称具有欺骗性;鉴于其 ob_ 前缀,任何有理智的人都会期望它会影响“输出缓冲区”,ob_startob_flush 等也是如此。但是,事实并非如此; ob_implicit_flush()flush() 一样,影响服务器级输出缓冲区,并且不会以任何方式与其他 ob_< 控制的输出缓冲区交互/代码> 功能。

第二种是通过设置 < 全局启用隐式刷新在 php.ini 中将 code>implicit_flush 标志设置为 On。这相当于在每个脚本开始时调用ob_implicit_flush()。请注意,该手册建议不要这样做,隐晦地引用了“严重的性能影响”,其中一些我在切线相关的答案

tl;dr version

Do two things:

  1. Disable the userspace output buffer, either...

    • Globally, by either...

      • Turning off output_buffering in your php.ini, or

      • Turning off output_buffering in your Apache config using

          php_flag "output_buffering" Off
        
    • or for just the script you care about, by either...

      • calling ob_end_flush(), or
      • calling ob_end_clean()
  2. Also, disable the server-level output buffer as much as you possibly can, by either:

    • calling ob_implicit_flush() at the start of your script, or
    • calling flush() after every echo statement or other statement that adds output to the response body

Longer version

Confusingly, there are two layers of buffering that may be relevant and the PHP documentation does a poor job of distinguishing between the two.

The output buffer

The first layer is usually referred to by the PHP docs as the 'output buffer'. This layer of buffering only affects output to the body of the HTTP response, not the headers. You can turn on output buffering with ob_start(), and turn it off with ob_end_flush() or ob_end_clean(). You can also have all your scripts automatically start with output buffering on using the output_buffering option in php.ini.

The default value of this option for production versions of php.ini is 4096, which means that the first 4096 bytes of output will be buffered in the output buffer, at which point it will be flushed and output buffering is turned off.

You can disable this layer of buffering globally by setting output_buffering to Off in your php.ini file (or using

php_flag "output_buffering" Off

in your Apache config, if you're using Apache). Alternatively, you can disable it for a single script by calling ob_end_clean() or ob_end_flush() at the start of the script.

The write buffer, and the webserver buffer

Beyond the output buffer is what the PHP manual refers to as the 'write buffer', plus any buffering system your web server has. If you're using PHP with Apache through mod_php, and are not using mod_gzip, you can call flush() to flush these; with other backends, it might work too, although the manual is cagey about giving assurances:

Description

void flush ( void )

Flushes the write buffers of PHP and whatever backend PHP is using (CGI, a web server, etc). This attempts to push current output all the way to the browser with a few caveats.

flush() may not be able to override the buffering scheme of your web server and it has no effect on any client-side buffering in the browser. It also doesn't affect PHP's userspace output buffering mechanism. This means you will have to call both ob_flush() and flush() to flush the ob output buffers if you are using those.

There are also a couple of ways you can make PHP automatically call flush() every time you echo anything (or do anything else that echoes output to the response body).

The first is to call ob_implicit_flush(). Note that this function is deceptively named; given its ob_ prefix, any reasonable person would expect that it would affect the 'output buffer', as do ob_start, ob_flush etc. However, this is not the case; ob_implicit_flush(), like flush(), affects the server-level output buffer and does not interact in any way with the output buffer controlled by the other ob_ functions.

The second is to globally enable implicit flushing by setting the implicit_flush flag to On in your php.ini. This is equivalent to calling ob_implicit_flush() at the start of every script. Note that the manual advises against this, cryptically citing "serious performance implications", some of which I explore in this tangentially related answer.

迷鸟归林 2025-01-03 15:32:20

您可以在每次之后调用 flush(),而不是禁用输出缓冲读操作。这可以避免弄乱服务器配置并使您的脚本更加可移植。

Rather than disabling output buffering, you can just call flush() after every read operation. This avoids having to mess with the server configuration and makes your script more portable.

一身骄傲 2025-01-03 15:32:20

输出缓冲可以分层,我曾经遇到过早期代码做了多个级别的情况。这将清除它们。

while (ob_get_level()) ob_end_clean(); 
// or ob_end_flush() if you want the contents of the buffer.

Output buffering can be layered and I've had cases where earlier code had made multiple levels. This will clear them all.

while (ob_get_level()) ob_end_clean(); 
// or ob_end_flush() if you want the contents of the buffer.
大海や 2025-01-03 15:32:20

我们可以在 .htaccess 文件中提供以下代码以禁用 PHP 中的输出缓冲

php_flag "output_buffering" off

We can give the below code in the .htaccess file for disabling output buffering in PHP

php_flag "output_buffering" off
錯遇了你 2025-01-03 15:32:20

对于 Windows IIS 服务器,上述解决方案都不起作用,因为 IIS 管理自己的缓冲区,也应该禁用它。

只需将以下 web.config 添加到带有 PHP 脚本的文件夹即可禁用缓冲。在下面的示例中使用 PHP v7.3.7,但您可以将名称替换为其他版本。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <clear />
            <add name="php-7.3.7" path="*.php" verb="GET,HEAD,POST" modules="FastCgiModule" scriptProcessor="C:\Program Files\PHP\v7.3\php-cgi.exe" resourceType="Either" requireAccess="Script" responseBufferLimit="0" />
        </handlers>
    </system.webServer>
</configuration>

For Windows IIS-servers none of solutions above work, because IIS manages its own buffers, which should be disabled too.

Just add the following web.config to your folder with PHP scripts to disable buffering. In example below used PHP v7.3.7, but you may replace the name to another version.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <clear />
            <add name="php-7.3.7" path="*.php" verb="GET,HEAD,POST" modules="FastCgiModule" scriptProcessor="C:\Program Files\PHP\v7.3\php-cgi.exe" resourceType="Either" requireAccess="Script" responseBufferLimit="0" />
        </handlers>
    </system.webServer>
</configuration>
自由范儿 2025-01-03 15:32:20

我知道这个问题有点老了,但是回到这个问题,您可以在脚本到脚本的基础上关闭输出缓冲,如下所示:

if (ob_get_level())
   ob_end_clean();

这应该关闭其后面的任何脚本的所有输出缓冲。

I know this question is a bit old, but coming back to this question, you can turn off output buffering on a script to script basis, like this:

if (ob_get_level())
   ob_end_clean();

This should turn off all output buffering for any script that follows it.

软糯酥胸 2025-01-03 15:32:20
// disable cache as much as possible
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header("Pragma: no-cache");
header("X-Accel-Buffering: no");
ob_implicit_flush(true);
// disable cache as much as possible
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header("Pragma: no-cache");
header("X-Accel-Buffering: no");
ob_implicit_flush(true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文