ฉันกำลังมองหาโซลูชันที่ช่วยให้สคริปต์ 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 อะไรบ้างเพื่อแก้ไขปัญหานี้ ความช่วยเหลือใดๆ จะได้รับการชื่นชมอย่างมาก...คำสั่งของฉันทำงานได้ดีที่บรรทัดคำสั่ง ดังนั้นนี่จึงเป็นสิ่งเดียวที่ทำให้แอปพลิเคชันของฉันหยุดทำงาน