How to get proc_open () output

I tried to get the output from the proc_open method in php, but when I printed 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);

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

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

But I can’t do it. any suggestion?

thanks before ...

+7
source share
2 answers

Your code more or less works for me. time outputs its output to stderr , so if you are looking for this output, look in your files/temp/error-output.txt . The pipe stdout $pipes[1] will contain only the output of the program ./a .

My reproducer:

 [ 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 
+6
source

this is another example with proc_open() . In this example, I use the Win32 ping.exe command. 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 =)

+4
source

All Articles