49

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)

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>"
}

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!

jww
  • 97,681
  • 90
  • 411
  • 885
Goran
  • 3,292
  • 2
  • 29
  • 32

3 Answers3

90

You need to capture the stderr too.

Redirecting stderr to stdout should do the trick. Append 2>&1 to the end of your command.

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

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
hakre
  • 193,403
  • 52
  • 435
  • 836
racetrack
  • 3,766
  • 30
  • 30
  • 14
    Aside: isn't it bizarre that PHP's exec doesn't provide a simple way to capture the `stderr` output separately? – Stephen C Jun 16 '13 at 02:31
  • @StephenC: It may appear so on first sight, however it makes more sense when understanding why there are two different streams firsthand ([some folklore](https://retrocomputing.stackexchange.com/q/11499)) and then knowing that PHP has a SAPI and error handling. E.g. in CLI when you execute that code, you directly see errors in your terminal - regardless of the output the program needs to process. Or if it's running within php-fpm, you get the error reporting in your server logs. Not such a bad default running a system. – hakre Jul 04 '21 at 13:03
0

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

$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);
Cedric
  • 5,135
  • 11
  • 42
  • 61
-3

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/');

ant
  • 22,634
  • 36
  • 132
  • 182