PHP exec() 不在输出中返回错误消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3863699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:20:16  来源:igfitidea点击:

PHP exec() not returning error message in output

phpxmlsvnexec

提问by Goran

I am trying to get certain output for svn command in XML format. Output is ok when I type valid parameters. However, when I type in wrong password, output does not show error message. This is the PHP code:

我正在尝试以 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);

Here is output I get in the terminal:

这是我在终端中得到的输出:

<?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)

And here is the output I get from the $output variable with var_dump:

这是我从带有 var_dump 的 $output 变量得到的输出:

array(2) {
[0]=>
string(21) "<?xml version="1.0"?>"
[1]=>
string(5) "<log>"
}

As you can see the $output variable does not return third line of output, where terminal does. Please help me to get the same output as I get in terminal (I even tried with shell_exec() or system() methods but they return the same output as exec()) How do I get full output? Thank you in advance!

如您所见, $output 变量不会返回第三行输出,而终端会返回。请帮助我获得与终端相同的输出(我什至尝试使用 shell_exec() 或 system() 方法,但它们返回与 exec() 相同的输出) 如何获得完整输出?先感谢您!

回答by racetrack

You need to capture the stderrtoo.

你也需要捕捉stderr

Redirecting stderrto stdoutshould do the trick. Append 2>&1to the end of your command.

重定向stderrstdout应该可以解决问题。附加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);

回答by Cedric

That is probably not the solution, just a bad suggestion : have you tried to add an extra echo in the command:

这可能不是解决方案,只是一个不好的建议:您是否尝试在命令中添加额外的回声:

exec('/usr/bin/svn --username something --password something --non-interactive log -r HEAD --xml --verbose http://a51.unfuddle.com/svn/a51_activecollab/;**echo ""**', $output);

And the other way to solve that is as already mentionned : if you don't have the last line in the $output, it is in the returned value of the exec() function anyway. You'd have then

解决这个问题的另一种方法是已经提到的:如果 $output 中没有最后一行,无论如何它都在 exec() 函数的返回值中。那时你会

$totalOutput = push($msg,$output);

with

$msg = exec('/usr/bin/svn --username something --password something --non-interactive log -r HEAD --xml --verbose http://a51.unfuddle.com/svn/a51_activecollab/', $output);

回答by ant

You need to define a variable in which to store the message

您需要定义一个变量来存储消息

$msg = exec('/usr/bin/svn --username something --password something --non-interactive log -r HEAD --xml --verbose http://a51.unfuddle.com/svn/a51_activecollab/');

$msg = exec('/usr/bin/svn --username something --password something --non-interactive log -r HEAD --xml --verbose http://a51.unfuddle.com/svn/a51_activecollab/');