我正在尝试以 XML 格式获取 svn 命令的某些输出。当我输入有效参数时输出正常。但是,当我输入错误的密码时,输出不会显示错误消息。
这是 PHP 代码:
exec('/usr/bin/svn --username something --password something --non-interactive log -r HEAD --xml --verbose http://a51.unfuddle.com/svn/a51_activecollab/', $output);
这是我在终端中得到的输出:
<?xml version="1.0"?>
<log>
svn: OPTIONS of 'http://a51.unfuddle.com/svn/a51_activecollab': authorization failed: Could not authenticate to server: rejected Basic challenge (http://a51.unfuddle.com)
这是我使用 var_dump 从 $output 变量获得的输出:
array(2) {
[0]=>
string(21) "<?xml version="1.0"?>"
[1]=>
string(5) "<log>"
}
正如您所看到的,$output 变量不返回第三行输出,而终端则返回第三行。请帮助我获得与终端中相同的输出(我什至尝试使用 shell_exec() 或 system() 方法,但它们返回与 exec() 相同的输出)如何获得完整输出?
先感谢您!
您需要捕获stderr
too.
重定向stderr
to stdout
应该可以解决问题。附加2>&1
到你的命令结束。
e.g.
exec("/usr/bin/svn --username something --password something --non-interactive log -r HEAD --xml --verbose http://a51.unfuddle.com/svn/a51_activecollab/ 2>&1", $output);
如果它是一个complex命令(例如带有管道的命令,例如执行 mysqldump 并将其传递给 gzip,然后重定向到文件mysqldump ... | gzip > db.sql.gz
) 创建一个子 shell 来捕获总体标准错误并将其重定向到标准输出:
exec('( error_command | cat >/dev/null ) 2>&1', $output)
# ^ ^ ^
# `-- sub-shell with the command --´ `-- stderr to $output
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)