0

我需要测试一些使用 stdout、stderr 并返回错误代码的 php cli 脚本。

  • exec似乎没有返回标准错误。
  • 系统不返回标准输出(仅最后一行)、标准错误。
4

1 回答 1

1

proc_open 可以使用。

文件:script.php

echo 'Standart output'; //stdout

error_log('Error output'); //stderr

exit(1); //return

文件: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;
    }
于 2013-09-03T04:25:17.783 回答