Я ищу решение, которое позволяет скрипту PHP отправлять несколько команд при появлении запроса. Когда следующий код выполняется из оболочки:
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, чтобы это исправить? Будем очень признательны за любую помощь... мои команды отлично работают в командной строке, так что это единственное, что сдерживает мое приложение.