You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP使用proc_open无法实时获取命令输出问题求助

PHP流式获取命令实时输出失败问题

我尝试使用PHP流式获取脚本的实时输出,在StackOverflow上查询了大量相关问题并参考了多个方案,但这些方案均无效,我总是在命令执行完成后才得到全部输出。以下是我使用的最终代码:

$cmd = "/path/to/command";

$descriptorspec = array(
   0 => array("pipe", "r"),   // stdin是子进程读取的管道
   1 => array("pipe", "w"),   // stdout是子进程写入的管道
   2 => array("pipe", "w")    // stderr是子进程写入的管道
);
flush();
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
echo "<pre>";
if (is_resource($process)) {
    while ($s = fgets($pipes[1])) {
        print $s;
        flush();
    }
}
echo "</pre>";

运行平台:Arch Linux + Apache

内容的提问来源于stack exchange,提问作者Puspam Adak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 23:15:33