Linux 如何获得 proc_open() 的输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6014819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get output of proc_open()
提问by Bobby Stenly
I've tried to get output from proc_open
method in php, but, when I print it, I got empty.
我试图从proc_open
php 中的方法获取输出,但是,当我打印它时,我得到了空。
$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);
as long as I know, I can get the output with stream_get_contents()
只要我知道,我就可以得到输出 stream_get_contents()
echo stream_get_contents($pipes[1]); fclose($pipes[1]);
But I can't do that.. any suggestion?
但我不能那样做..有什么建议吗?
Thx before...
Thx之前...
采纳答案by e.dan
Your code more or less works for me. time
prints its output to stderr
so if you're looking for that output, look in your file files/temp/error-output.txt
. The stdout
pipe $pipes[1]
will only contain the output of the program ./a
.
你的代码或多或少对我有用。 time
将其输出打印到,stderr
因此如果您正在寻找该输出,请查看您的文件files/temp/error-output.txt
。该stdout
管道$pipes[1]
将只包含该程序的输出./a
。
My repro:
我的重现:
[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
回答by Didit Dwianto
this is another example with proc_open()
.
I am using Win32 ping.exe command in this example. CMIIW
这是另一个例子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();
}
}
Hope this helps =)
希望这会有所帮助 =)