[PHP] Use of callback on a stream



Hello,

I am trying to make a daemon that launches shell commands via proc_open and gets the stdout of the command via a pipe. The thing is that I would like to get this stdout via a callback, instead of monitoring the pipe regularly.
I tried the following code (this is a simplified version of it), but the callback never gets called,

does anyone know what I forgot ?
Thanks,
Olivier


<?php
// Php5 used

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

// We make a simple callback function with an ugly global var
function stream_callback(){
global $pipes;

$stdout_child=stream_get_contents($pipes[1]);
print("Callback received:\n");
print($stdout_child."\n");
}

$context=stream_context_create(array());
stream_context_set_params($context,array('notification' => 'stream_callback'));
$handle=proc_open("/bin/ls /tmp",$descriptorspec,$pipes,null,null,array('context' => $context));
stream_context_set_params($pipes[1],array('notification' => 'stream_callback'));

while (true){
print("Waiting...\n");
sleep(5);
}




.



Relevant Pages