15

我试图从proc_openphp 中的方法获取输出,但是,当我打印它时,我得到了空。

$descriptorspec = 数组(
    0 => 数组(“管道”,“r”),
    1 => 数组(“管道”,“w”),
    2 => 数组(“文件”、“文件/临时/错误输出.txt”、“a”)
);

$process = proc_open("time ./a a.out", $descriptorspec, $pipes, $cwd);

只要我知道,我就可以得到输出stream_get_contents()

echo stream_get_contents($pipes[1]);
fclose($pipes[1]);

但我不能这样做..有什么建议吗?

谢谢之前...

4

2 回答 2

11

您的代码或多或少对我有用。 time将其输出打印到,stderr因此如果您正在寻找该输出,请查看您的文件files/temp/error-output.txtstdout管道将$pipes[1]只包含程序的输出./a

我的复制品:

[edan@edan tmp]$ cat proc.php 

<?php

$cwd='/tmp';
$descriptorspec = array(
    0 => array("pipe", "r"),
    1 => array("pipe", "w"),
    2 => array("file", "/tmp/error-output.txt", "a") );

$process = proc_open("time ./a a.out", $descriptorspec, $pipes, $cwd);

echo stream_get_contents($pipes[1]);
fclose($pipes[1]);

?>

[edan@edan tmp]$ php proc.php 

a.out here.

[edan@edan tmp]$ cat /tmp/error-output.txt

real    0m0.001s
user    0m0.000s
sys     0m0.002s
于 2011-05-16T11:34:03.967 回答
10

这是另一个例子proc_open()。我在这个例子中使用 Win32 ping.exe 命令。CMIIW

set_time_limit(1800);
ob_implicit_flush(true);

$exe_command = 'C:\\Windows\\System32\\ping.exe -t google.com';

$descriptorspec = array(
    0 => array("pipe", "r"),  // stdin
    1 => array("pipe", "w"),  // stdout -> we use this
    2 => array("pipe", "w")   // stderr 
);

$process = proc_open($exe_command, $descriptorspec, $pipes);

if (is_resource($process))
{

    while( ! feof($pipes[1]))
    {
        $return_message = fgets($pipes[1], 1024);
        if (strlen($return_message) == 0) break;

        echo $return_message.'<br />';
        ob_flush();
        flush();
    }
}

希望这会有所帮助=)

于 2013-03-25T04:44:33.613 回答