我的PHP网络应用程序从流中接收数据。加载页面后,我需要使用.exe
或system()
打开exec()
文件,并在短时间内出现新数据,因此我必须键入特定命令{{1获取其返回值,我该怎么做?
我只能在命令提示符下手动执行此操作
.exe
答案 0 :(得分:1)
您正在寻找的是proc_open()
。 http://php.net/manual/en/function.proc-open.php
这将允许您使用STDIO流与单独的进程通信。
$descriptorspec = 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("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);
$cwd = '/tmp';
$env = array('some_option' => 'aeiou');
$process = proc_open('php', $descriptorspec, $pipes, $cwd, $env);
if (is_resource($process)) {
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// Any error output will be appended to /tmp/error-output.txt
fwrite($pipes[0], '<?php print_r($_ENV); ?>');
fclose($pipes[0]);
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return_value = proc_close($process);
echo "command returned $return_value\n";
}
答案 1 :(得分:1)
如果您需要多个侦听器,您还可以考虑使用共享内存,但这种情况听起来好像您可以从使用队列中受益。
文档msg_get_queue
,msg_receive
,msg_send
示例强>
// Send
if (msg_queue_exists(12345)) {
$mqh = msg_get_queue(12345);
$result = msg_send($mqh , 1, 'data', true);
}
// Receive
$mqh = msg_get_queue(12345, 0666);
$mqst = msg_stat_queue($mqh);
while ($mqst['msg_qnum']) {
msg_receive($mqh, 0, $msgtype, 2048, $data, true);
// Spawn your process
$mqst = msg_stat_queue($mqh);
}
修改强>