php - 压缩手动关闭的连接并继续处理
问题描述
- 我已经设法尽早关闭 PHP 连接并继续处理。
- 我已经设法向客户发送压缩响应。
- 我没能同时做到这两点。
如何发送压缩响应并继续处理?
目前我的最小测试用例压缩了服务器响应,尽管它没有关闭连接:
<?php
ob_start();
echo '<style type="text/css">* {background-color: #000; color: #fff;}</style>';
echo '<p>Testing response: '.time().'.</p>';
$c = ob_get_contents();
ob_end_clean();
ob_start('ob_gzhandler');
echo $c;
$size = ob_get_length();
ob_end_flush();
// Set the content length of the response.
header("Content-Length: {$size}");
// Close the connection.
header('Connection: close');
// Flush all output.
ob_end_flush();
// Close current session (if it exists).
if (session_id()) {session_write_close();}
sleep(2);
file_put_contents('test.txt', 'testing: '.time().'; size: '.$size);
?>
解决方案
由于 Rush 在ob_get_length 文档页面上的评论,我设法减少了代码。所有三个刷新命令都是必需的,注释掉其中任何一个都会导致页面直到 after 才加载sleep(4)
。我对此进行了测试以确保连接在浏览器中关闭,被压缩,然后切换到我的文件管理器以查看几秒钟后创建的文件。
<?php
ob_start();
ob_start('ob_gzhandler');
// Send your response.
echo '<style>* {background-color: #000; color: #fff;}</style>';
echo '<p>Testing response: '.time().'.</p>';
// The ob_gzhandler one
ob_end_flush();
header('Content-Length: '.ob_get_length());
// The main one
ob_end_flush();
ob_flush();
flush();
// Close current session (if it exists).
if (session_id()) {session_write_close();}
sleep(4);
file_put_contents('test.txt', 'testing: '.time().'; size: '.$size);
?>
推荐阅读
- android - 从 ImageView Android 的左边缘裁剪
- java - 为什么我的数据库没有正确更新我的值?
- php - 不显示我的参考链接
- python - 仅对时间序列中的某些数据点应用校正,这些数据点通过值快速下降来识别
- python - ValueError:输入数组应具有与目标数组相同数量的样本。找到 1600 个输入样本和 1800 个目标样本
- xml - ant 编译没有显示类
- c# - Unity:如何将 AssetBundle 构建到每个 AssetBundle 的单独目录中
- javascript - Discord.js 和 discord.js-commando 防止命令在特定通道中运行
- c# - 将 aspnet 核心作为服务运行 - nssm 与 RunAsService
- merge - SAS:合并具有不同名称的相同变量的两个数据集