在 PHP 中,我使用 exec() 执行命令,如果成功则返回 URL;
$url = exec('report');
但是,如果出现问题,我想检查 stderr。我将如何阅读流?我想使用 php://stderr,但我不知道如何使用它。
如果您想执行一个命令,并同时获得stderr
and stdout
,而不是“合并”,则可能会使用一种解决方案proc_open
,它可以对正在执行的命令提供高度控制——包括一种管道stdin
/ stdout
/的方法stderr
。
下面是一个例子:假设我们有这个 shell 脚本 in test.sh
,它同时写入stderr
和stdout
:
#!/bin/bash
echo 'this is on stdout';
echo 'this is on stdout too';
echo 'this is on stderr' >&2;
echo 'this is on stderr too' >&2;
现在,让我们编写一些 PHP 代码,temp.php
首先,我们初始化 i/o 描述符:
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w"), // stderr
);
然后,test.sh
使用这些描述符在当前目录中执行命令,并说 i/o 应该是 from/to $pipes
:
$process = proc_open('./test.sh', $descriptorspec, $pipes, dirname(__FILE__), null);
我们现在可以从两个输出管道中读取:
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
而且,如果我们输出这两个变量的内容:
echo "stdout : \n";
var_dump($stdout);
echo "stderr :\n";
var_dump($stderr);
temp.php
执行脚本时我们得到以下输出:
$ php ./temp.php
stdout :
string(40) "this is on stdout
this is on stdout too
"
stderr :
string(40) "this is on stderr
this is on stderr too
"
一个可能有用的小功能:
function my_shell_exec($cmd, &$stdout=null, &$stderr=null) {
$proc = proc_open($cmd,[
1 => ['pipe','w'],
2 => ['pipe','w'],
],$pipes);
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
return proc_close($proc);
}
返回退出代码,如果需要,STDOUT 和 STDERR 是参考参数。
做这样的事情的捷径exec
是返回退出代码(命令的状态)
请注意,我正在尝试列出一个不存在的目录/non-dir/
exec('ls /non-dir/', $out, $retval);
var_dump($retval);
输出
ls: 无法访问'/non-dir/': 没有这样的文件或目录
int(2)
通常在基于 unix 的系统中,大多数成功状态代码是 ( 0 ),因此您可以检查您$retval
的命令以了解命令的状态。
要消除列出无效路径的错误,ls: cannot access '/non-dir/': No such file or directory
您可以将stderr重定向到 null
exec('ls /non-dir/ 2>/dev/null', $out, $retval);
var_dump($retval);
这将输出:
整数(2)
此外,如果您需要错误字符串在任何情况下使用它,您可以将您的stderr重定向到stdout。
exec('ls /non-dir/ 2>&1', $out, $retval);
print_r($out);
var_dump($retval);
这将输出以下内容:
Array
(
[0] => ls: cannot access '/non-dir/': No such file or directory
)
int(2)
另一种获得未合并标准输出/标准错误的方法。
$pp_name = "/tmp/pp_test";
@unlink($pp_name);
posix_mkfifo($pp_name, 0777);
$pp = fopen($pp_name, "r+");
stream_set_blocking($pp, FALSE);
exec("wget -O - http://www.youtube.com 2>$pp_name", $r_stdout);
$r_stderr = stream_get_contents($pp);
var_dump($r_stderr);
fclose($pp);
unlink($pp_name);
如果您想忽略 stdout 并仅获取 stderr,可以尝试以下操作:
exec("wget -O - http://www.youtube.com 2>&1 >/dev/null", $r_stderr);
exec("{$command} 2>&1"
,$output
,$exitCode
);
2>&1将标准错误重定向到标准输出以获得一致的成功/失败行为。
$exitCode确定$command完成状态。
$output包含与$exitCode关联的所有输出。
有点丑,但足够好。将 stderr 放入临时文件并读回。
$tmp = tempnam("/tmp", "ERR_");
exec('report 2> ' . escapeshellarg($tmp), $stdout, $retcode);
$stderr = file_get_contents($tmp);
unlink($tmp);
if ($retcode == 0)
{
// good
$url = $stdout[0];
} else {
// bad
error_log("FAIL: $stderr");
}