I need to test some php cli script which uses stdout, stderr and return error code.
Asked
Active
Viewed 611 times
0
-
I would advice [`proc_open`](http://php.net/proc_open) then. It's a bit unwieldy though :) – Ja͢ck Sep 03 '13 at 04:06
-
Duplicate of http://stackoverflow.com/questions/2320608/php-stderr-after-exec ? – hendry Sep 03 '13 at 04:19
1 Answers
1
proc_open could be used.
File: script.php
echo 'Standart output'; //stdout
error_log('Error output'); //stderr
exit(1); //return
File: test.php
<?php
$descriptorspec = array(
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a pipe that the child will write to
);
$process = proc_open('php script.php', $descriptorspec, $pipes);
if (is_resource($process))
{
echo 'stdout: ' . stream_get_contents($pipes[1]) . PHP_EOL;
fclose($pipes[1]);
echo 'stderr: ' . stream_get_contents($pipes[2]) . PHP_EOL;
fclose($pipes[2]);
$return_value = proc_close($process);
echo 'return: ' . $return_value . PHP_EOL;
}

sectus
- 15,605
- 5
- 55
- 97