听起来像是一份工作proc_open
!
您将向它传递要运行的命令,然后是包含要打开的流的描述的数组,以表示该进程的标准输入、标准输出和标准错误。
流实际上是文件句柄,因此您可以像写入文件一样简单地写入它们。
例如,从我自己的代码库中的打印位:
// 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);