Похоже на задание для proc_open
!
Вы передадите ему команду для запуска, затем массив, содержащий описания потоков, которые нужно открыть для представления stdin, stdout и stderr дляпроцесс.
Потоки по сути являются файловыми дескрипторами, поэтому вы можете просто писать в них, как если бы вы писали в файл.
Например, из битов печати в моей собственной кодовой базе:
// In this case, $data is a PDF document that we'll feed to
// the stdin of /usr/bin/lp
$data = '';
$handles = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "a") // stderr is a file to write to
);
// Setting of $server, $printer_name, $options_flag omitted...
$process_name = 'LC_ALL=en_US.UTF-8 /usr/bin/lp -h %s -d %s %s';
$command = sprintf($process_name, $server, $printer_name, (string)$options_flag);
$pipes = array();
$process = proc_open($command, $handles, $pipes);
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// As we've been given data to write directly, let's kinda like do that.
fwrite($pipes[0], $data);
fclose($pipes[0]);
// 1 => readable handle connected to child stdout
$stdout = fgets($pipes[1]);
fclose($pipes[1]);
// 2 => readable handle connected to child stderr
$stderr = fgets($pipes[2]);
fclose($pipes[2]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);