开发者

PHP: Executing Command Line Application That Prompts Users

开发者 https://www.devze.com 2023-01-05 12:31 出处:网络
I have a command line application I need to execute from my 开发者_JS百科PHP web application. Say the command is the following:

I have a command line application I need to execute from my 开发者_JS百科PHP web application. Say the command is the following:

foo -arg1 -arg2 -arg3

Based on certain conditions, the command line application will prompt user to hit the enter key (e.g. "Please press enter to continue.").

From my PHP application, how do I execute the command line AND send the enter key as response to the prompt?

I'm developing on WAMP. Production code is LAMP.


That's what the 'yes' program is for. It dumps an endless stream of 'y\n' (or whatever you tell it to via arguments) to the program. It exists for this purpose (answering 'yes' to "do you want to continue" prompts).

shell_exec('yes | foo -arg1 -arg2 -arg3')


You will really need to open a process handle and parse the programs output and write appropriate output in response.

Check out the expect extension though, which can make this sort of thing easier.


$value = fgets(STDIN);

This will allow the user to enter in a value, which you can then access via $value.


Have you tried echo "\n" > foo -arg1 -arg2 -arg3 ?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号