Skip to content

Commit 16f8eb4

Browse files
committed
Create proc.php
1 parent 0b54460 commit 16f8eb4

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

examples/php/proc.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
$descriptorspec = array(
3+
0 => array("pipe", "r"), // 标准输入,子进程从此管道中读取数据
4+
1 => array("pipe", "w"), // 标准输出,子进程向此管道中写入数据
5+
2 => array("file", "/tmp/error-output.txt", "a") // 标准错误,写入到一个文件
6+
);
7+
8+
$cwd = '/tmp';
9+
$env = array('some_option' => 'aeiou');
10+
11+
$process = proc_open('php', $descriptorspec, $pipes, $cwd, $env);
12+
13+
if (is_resource($process)) {
14+
// $pipes 现在看起来是这样的:
15+
// 0 => 可以向子进程标准输入写入的句柄
16+
// 1 => 可以从子进程标准输出读取的句柄
17+
// 错误输出将被追加到文件 /tmp/error-output.txt
18+
19+
fwrite($pipes[0], '<?php print_r($_ENV); ?>');
20+
fclose($pipes[0]);
21+
22+
echo stream_get_contents($pipes[1]);
23+
fclose($pipes[1]);
24+
25+
26+
// 切记:在调用 proc_close 之前关闭所有的管道以避免死锁。
27+
$return_value = proc_close($process);
28+
29+
echo "command returned $return_value\n";
30+
}

0 commit comments

Comments
 (0)