There doesn't appear to be a way to get data from BOTH stderr and stdout streams simultaneously, or at least I've yet to find one.
The following code *should* get the error message written to stderr, but if the call to stream_get_contents() for stdout is run first, the subsequent call for stderr won't return anything.
If the order of the statements is reversed, the call for stderr will return any errors and call for stdout will return nothing
<?php
$stream = ssh2_exec($connection, "cd /hom");
$errorStream = ssh2_fetch_stream($stream, SSH2_STREAM_STDERR);
stream_set_blocking($errorStream, true);
stream_set_blocking($stream, true);
echo "Output: " . stream_get_contents($stream);
echo "Error: " . stream_get_contents($errorStream);
fclose($errorStream);
fclose($stream);
?>
My initial suspicion is that either a) I've done something wrong or b) the blocking nature of both streams means that by the time first stream has received data and returned, the buffer for the second stream has already emptied.
I've done preliminary tests with blocking disabled, but haven't had any luck there either.