tags:

views:

40

answers:

2

In PHP i am executing a command with exec(), and it returns if successful an URL;

$url = exec('report');

However, I want to check STD_ERR, if something went wrong. How would I read the stream? I want to use php://stderr, but not sure how to use it.

Thanks, Martin

+1  A: 

According to this, it's

 $url = exec("$com 2>&1", $out, $err); 

has always worked for me.

Pekka
Yes, that's what I saw as well. Was not sure if this would really return a value to $url, since it is redirecting stderr to stdout?
martin
@martin: Are you telling me you saw this, but instead of taking a _trivial_ amount of time to try it out you asked on StackOverflow?
hobodave
I saw this, yes. But since it states:>It's not very clean, since it mixes stderr with stdoutI wanted to see if there is a cleaner solution. Sorry, should have made a reference to this as well.
martin
+1  A: 

If you want to execute a command, and get both stderr and stdout, not "merged", a solution would probably to use proc_open, which provides a great level of control over the command that's being executed -- including a way to pipe stdin/stdout/stderr.


And here is an example : let's consider we have this shell-script, in test.sh, which writes to both stderr and 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;


Now, let's code some PHP, in temp.php -- first, we initialize the i/o descriptors :

$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin
   1 => array("pipe", "w"),  // stdout
   2 => array("pipe", "w"),  // stderr
);


And, then, execute the test.sh command, using those descriptors, in the current directory, and saying the i/o should be from/to $pipes :

$process = proc_open('./test.sh', $descriptorspec, $pipes, dirname(__FILE__), null);


We can now read from the two output pipes :

$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);

$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);


And, if we output the content of those two variables :

echo "stdout : \n";
var_dump($stdout);

echo "stderr :\n";
var_dump($stderr);


We get the following output when executing the temp.php script :

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


Hope this helps :-)

Pascal MARTIN