我正在寻找一种允许 PHP 脚本在出现提示时发送多个命令的解决方案。当从 shell 执行以下代码时:
root@host [~]# /usr/local/bin/grads-2.0.2/bin/grads -b
此输出结果:
Grid Analysis and Display System (GrADS) Version 2.0.2
Copyright (c) 1988-2011 by Brian Doty and the
Institute for Global Environment and Society (IGES)
GrADS comes with ABSOLUTELY NO WARRANTY
See file COPYRIGHT for more information
Config: v2.0.2 little-endian readline printim grib2 netcdf hdf4-sds hdf5 opendap-grids,stn geotiff shapefile
Issue 'q config' command for more detailed configuration information
Landscape mode? ('n' for portrait):
如您所见,脚本正在等待 y/n 输入。当输入 y/n 时,输出结果如下:
Landscape mode? ('n' for portrait): y
GX Package Initialization: Size = 11 8.5
Running in Batch mode
ga->
然后脚本等待任何进一步的命令,直到可以使用“quit”命令退出。'quit' 会产生以下输出:
ga-> quit
No hardcopy metafile open
GX package terminated
root@host [~]#
但是,当我尝试通过 PHP 执行此操作时,就会出现我的问题。我的代码如下:
$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 = '/';
$process = proc_open('/usr/local/bin/grads-2.0.2/bin/grads -b', $descriptorspec, $pipes, $cwd);
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], 'y');
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);
}
但是,这是一次输出:
Grid Analysis and Display System (GrADS) Version 2.0.2
Copyright (c) 1988-2011 by Brian Doty and the
Institute for Global Environment and Society (IGES)
GrADS comes with ABSOLUTELY NO WARRANTY
See file COPYRIGHT for more information
Config: v2.0.2 little-endian readline printim grib2 netcdf hdf4-sds hdf5 opendap-grids,stn geotiff shapefile
Issue 'q config' command for more detailed configuration information
Landscape mode? ('n' for portrait): GX Package Initialization: Size = 11 8.5
Running in Batch mode
ga-> [EOF]
No hardcopy metafile open
GX package terminated
如您所见,脚本在不等待输入的情况下运行...我需要对 PHP 代码进行哪些修改才能解决此问题?任何帮助将不胜感激......我的命令在命令行上运行良好,所以这是唯一阻止我的应用程序的事情。