In PHP 5.4.24 and 5.4.25, this command does not correctly return the stream blocking status. It always returns ['blocked'] == 1 regardless of the actual blocking mode. A call to stream_set_blocking($stream, 0) will succeed (return TRUE) and subsequent calls to stream_get_contents($stream) will NOT block, even though a call to stream_get_meta_data($stream) will return 'blocked' == 1. Hopefully this will save some people a bunch of debugging time.
See bug report #47918 for more information (http://bugs.php.net/bug.php?id=47918).
Proof:
<?php
$d = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('file', 'error.log', 'a')
);
$p = proc_open('php -S localhost:8000', $d, $pipes);
if (!is_resource($p)) die("proc_open() failed\n");
// Set child's stdout pipe to non-blocking.
if (!stream_set_blocking($pipes[1], 0)) {
die("stream_set_blocking() failed\n");
}
else {
echo "Non-blocking mode should be set.\n";
}
// View the status of that same pipe.
// Note that 'blocked' is 1! This appears to be wrong.
print_r(stream_get_meta_data($pipes[1]));
// Try to read something. This will block if in blocking mode.
// If it does not block, stream_set_blocking() worked but
// stream_get_meta_data() is lying about blocking mode.
$data = stream_get_contents($pipes[1]);
echo "data = '$data'\n";
?>
Output:
Non-blocking mode should be set.
Array
(
[stream_type] => STDIO
[mode] => r
[unread_bytes] => 0
[seekable] =>
[timed_out] =>
[blocked] => 1 // << claims to be in blocking mode
[eof] =>
)
data = '' // this would never appear if we blocked.