2011-05-16 73 views
7

我试图从proc_open方法在PHP中获得输出,但是,当我打印它时,我变空了。如何获得输出proc_open()

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

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

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

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

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

THX前...

+0

哈哈,你的代码实际上让我明白了如何proc_open的作品。 – Yoshiyahu 2012-05-03 19:24:30

+0

@Yoshiyahu哈哈,很高兴知道.. :) – 2012-06-08 08:49:42

回答

6

您的代码或多或少对我的作品。 time将其输出打印到stderr,所以如果您要查找该输出,请查看您的文件files/temp/error-output.txtstdout管道$pipes[1]将仅包含程序./a的输出。

我的摄制:

[[email protected] 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]); 

?> 

[[email protected] tmp]$ php proc.php 

a.out here. 

[[email protected] tmp]$ cat /tmp/error-output.txt 

real 0m0.001s 
user 0m0.000s 
sys  0m0.002s 
+0

Thx对你的帮助... 我不检查我的stderr ... 终于,我检查了它,发现输出... 谢谢.. – 2011-05-16 13:18:49

4

这是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(); 
    } 
} 

希望这有助于=)

+0

为什么你需要ob_flush并冲洗? – nidhimj22 2016-02-24 14:37:31

+0

总结:“flush()可能无法覆盖您的web服务器的缓冲方案,并且它不会影响浏览器中的任何客户端缓冲,也不会影响PHP的用户空间输出缓冲机制,这意味着如果你正在使用ob_flush()和flush()来刷新ob输出缓冲区,你将不得不调用它。“ 来源:http://www.php.net/manual/en/function.flush.php – 2016-12-22 08:19:47