如何获取proc_open()的输出 [英] how to get output of proc_open()

查看:127
本文介绍了如何获取proc_open()的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从php中的proc_open方法获取输出,但是,当我打印它时,我却空了.

I've tried to get output from proc_open method in php, but, when I print it, I got empty.


$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()

as long as I know, I can get the output with stream_get_contents()


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

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

But I can't do that.. any suggestion?

谢谢...

推荐答案

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

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.

我的复制人:

[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

这篇关于如何获取proc_open()的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆