私の PHP Web アプリは、ストリームからデータを受け取ります。ページが読み込まれたら、orを.exe
使用してファイルを開く必要があり、しばらくすると新しいデータが表示されるため、特定のコマンドを入力して戻り値を取得する必要があります。これを行うにはどうすればよいですか?system()
exec()
.exe
コマンドプロンプトで手動でしかこれを行うことができません
path/to/.exe :: hit 'Enter'
command1 params1
//...
複数のリスナーが必要な場合は共有メモリを検討することもできますが、このシナリオでは、キューを使用することでメリットが得られるようです。
ドキュメント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);
}
編集
セマフォ機能はWindowsでは使用できません。上記で提案したように、最善の策はpopen
(単方向)またはproc_open
双方向のサポートを使用することです。
あなたが探しているのはです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";
}