Skip to content

Commit

Permalink
Create proc.php
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Jun 4, 2019
1 parent 0b54460 commit 16f8eb4
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/php/proc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
$descriptorspec = array(
0 => array("pipe", "r"), // 标准输入,子进程从此管道中读取数据
1 => array("pipe", "w"), // 标准输出,子进程向此管道中写入数据
2 => array("file", "/tmp/error-output.txt", "a") // 标准错误,写入到一个文件
);

$cwd = '/tmp';
$env = array('some_option' => 'aeiou');

$process = proc_open('php', $descriptorspec, $pipes, $cwd, $env);

if (is_resource($process)) {
// $pipes 现在看起来是这样的:
// 0 => 可以向子进程标准输入写入的句柄
// 1 => 可以从子进程标准输出读取的句柄
// 错误输出将被追加到文件 /tmp/error-output.txt

fwrite($pipes[0], '<?php print_r($_ENV); ?>');
fclose($pipes[0]);

echo stream_get_contents($pipes[1]);
fclose($pipes[1]);


// 切记:在调用 proc_close 之前关闭所有的管道以避免死锁。
$return_value = proc_close($process);

echo "command returned $return_value\n";
}

0 comments on commit 16f8eb4

Please sign in to comment.