問題描述
我正在嘗試以 XML 格式獲取 svn 命令的某些輸出.當我輸入有效參數時,輸出正常.但是,當我輸入錯誤的密碼時,輸出不會顯示錯誤消息.這是 PHP 代碼:
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:
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)
這是我從帶有 var_dump 的 $output 變量得到的輸出:
And here is the output I get from the $output variable with var_dump:
array(2) {
[0]=>
string(21) "<?xml version="1.0"?>"
[1]=>
string(5) "<log>"
}
如您所見,$output 變量不會返回第三行輸出,而終端會返回.請幫助我獲得與終端相同的輸出(我什至嘗試使用 shell_exec() 或 system() 方法,但它們返回與 exec() 相同的輸出) 如何獲得完整輸出?提前謝謝你!
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!
推薦答案
您也需要捕獲 stderr
.
將 stderr
重定向到 stdout
應該可以解決問題.將 2>&1
附加到您的命令末尾.
Redirecting stderr
to stdout
should do the trick. Append 2>&1
to the end of your command.
例如
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);
如果它是一個復雜命令(例如一個帶有管道的命令,比如做一個 mysqldump 并將它傳遞給 gzip 然后重定向到文件 mysqldump ... | gzip > db.sql.gz
) 創建一個子shell來捕獲整個標準錯誤并將其重定向到標準輸出:
If it is a complex command (e.g. one with a pipe, like doing a mysqldump and passing it to gzip and then redirecting to file mysqldump ... | gzip > db.sql.gz
) create a subshell to capture the overall standard-error and redirect it to standard-output:
exec('( error_command | cat >/dev/null ) 2>&1', $output)
# ^ ^ ^
# `-- sub-shell with the command --′ `-- stderr to $output
這篇關于PHP exec() 不在輸出中返回錯誤消息的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!