如何使用proc_open()函数通过管道连接到PHP进程?
在本例中,有两个PHP文件,stdin.PHP(子进程组件)和proc_open.PHP(父进程组件),都存储在域的public_html的同一文件夹中。还有一个程序X,它将数据导入stdin.php如何使用proc_open()函数通过管道连接到PHP进程?,php,pointers,pipe,stdin,Php,Pointers,Pipe,Stdin,在本例中,有两个PHP文件,stdin.PHP(子进程组件)和proc_open.PHP(父进程组件),都存储在域的public_html的同一文件夹中。还有一个程序X,它将数据导入stdin.php stdin.php(此组件工作正常) 这是一个不应该通过浏览器执行的过程,因为它注定要从程序X接收输入,并将其全部备份到一个文件(名为stdin_backup)中此过程正在运行,因为每次程序X管道输入时,该过程都会对其进行完全备份。如果在未传递输入的情况下执行此进程(例如从浏览器执行),则该进程
stdin.php(此组件工作正常) 这是一个不应该通过浏览器执行的过程,因为它注定要从程序X接收输入,并将其全部备份到一个文件(名为stdin_backup)中此过程正在运行,因为每次程序X管道输入时,该过程都会对其进行完全备份。如果在未传递输入的情况下执行此进程(例如从浏览器执行),则该进程将创建一个带有文本“error”的文件(名为stdin_error) 下面,部分代码是由于流程正常工作而被忽略的(如上所述)。所示代码仅用于说明:
#!/usr/bin/php -q
<?php
// Get the input
$fh_stdin = fopen('php://stdin', 'rb');
if (is_resource($fh_stdin)) {
// ... Code that backs up STDIN in a file ...
} else {
// ... Code that logs "ERROR" in a file ...
}
?>
我不确定传递给proc_open()
的命令是否正确,因为我没有找到这种情况的示例,如上所述,此脚本失败。我不知道proc_open.php还有什么不正确的地方
另外,当我执行proc_open.php进程时,会产生一个无限循环,反复打印下一个字符串:
X-Powered-By:PHP/5.5.20内容类型:text/html;字符集=UTF-8
相反,尝试了
popen('php.\uu DIR\uu.'/stdin.php','w')
,得到了完全相同的结果:打印相同字符串的无限循环错误,没有错误,没有日志,也没有stdin.php执行的信号。如果我正确理解了您的问题,您希望打开一个进程并将数据写入该进程的stdin流。您可以使用该功能进行以下操作:
$descriptors = array(
0 => array("pipe", "r"), // STDIN
1 => array("pipe", "w"), // STDOUT
2 => array("pipe", "w") // STDERR
);
$proc = proc_open("php script2.php", $descriptors, $pipes);
fwrite($pipes[0], "Your data here...");
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$exitCode = proc_close($proc);
如果您只是想测试第二个脚本,那么简单地使用shell命令可能会更容易:
$ echo "your data" | php script2.php
或者
$ php script2.php < datafile.txt
如果我正确理解了您的问题,您希望打开一个进程并将数据写入该进程的STDIN流。您可以使用该功能进行以下操作:
$descriptors = array(
0 => array("pipe", "r"), // STDIN
1 => array("pipe", "w"), // STDOUT
2 => array("pipe", "w") // STDERR
);
$proc = proc_open("php script2.php", $descriptors, $pipes);
fwrite($pipes[0], "Your data here...");
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$exitCode = proc_close($proc);
如果您只是想测试第二个脚本,那么简单地使用shell命令可能会更容易:
$ echo "your data" | php script2.php
或者
$ php script2.php < datafile.txt
如果我正确理解了您的问题,您希望打开一个进程并将数据写入该进程的STDIN流。您可以使用该功能进行以下操作:
$descriptors = array(
0 => array("pipe", "r"), // STDIN
1 => array("pipe", "w"), // STDOUT
2 => array("pipe", "w") // STDERR
);
$proc = proc_open("php script2.php", $descriptors, $pipes);
fwrite($pipes[0], "Your data here...");
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$exitCode = proc_close($proc);
如果您只是想测试第二个脚本,那么简单地使用shell命令可能会更容易:
$ echo "your data" | php script2.php
或者
$ php script2.php < datafile.txt
如果我正确理解了您的问题,您希望打开一个进程并将数据写入该进程的STDIN流。您可以使用该功能进行以下操作:
$descriptors = array(
0 => array("pipe", "r"), // STDIN
1 => array("pipe", "w"), // STDOUT
2 => array("pipe", "w") // STDERR
);
$proc = proc_open("php script2.php", $descriptors, $pipes);
fwrite($pipes[0], "Your data here...");
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
$exitCode = proc_close($proc);
如果您只是想测试第二个脚本,那么简单地使用shell命令可能会更容易:
$ echo "your data" | php script2.php
或者
$ php script2.php < datafile.txt