How do I write a command-line interactive PHP scri

2020-02-17 05:27发布

I want to write a PHP script that I can use from the command line. I want it to prompt and accept input for a few items, and then spit out some results. I want to do this in PHP, because all my classes and libraries are in PHP, and I just want to make a simple command line interface to a few things.

The prompting and accepting repeated command line inputs is the part that's tripping me up. How do I do this?

标签: php shell
9条回答
放荡不羁爱自由
2楼-- · 2020-02-17 06:09

One line of code (line 2):

<?php
    $name = trim(shell_exec("read -p 'Enter your name: ' name\necho \$name"));
    echo "Hello $name, this is PHP speaking\n";
    exit;

Checkout this answer's source in blog post How can I capture user input from the cmd line using PHP?.

查看更多
欢心
3楼-- · 2020-02-17 06:15

Simple:

#!/usr/bin/php
<?php
define('CONFIRMED_NO', 1);

while (1) {
    fputs(STDOUT, "\n"."***WARNING***: This action causes permanent data deletion.\nAre you sure you're not going to wine about it later? [y,n]: ");

    $response = strtolower(trim(fgets(STDIN)));
    if( $response == 'y' ) {
        break;
    } elseif( $response == 'n' ) {
        echo "\n",'So I guess you changed your mind eh?', "\n";
        exit (CONFIRMED_NO);
    } else {
        echo "\n", "Dude, that's not an option you idiot. Let's try this again.", "\n";
        continue;
    }
}

echo "\n","You're very brave. Let's continue with what we wanted to do.", "\n\n";
查看更多
孤傲高冷的网名
4楼-- · 2020-02-17 06:16

I'm not sure how complex your input might be, but readline is an excellent way to handle it for interactive CLI programs.

You get the same creature comforts out of it that you would expect from your shell, such as command history.

Using it is as simple as:

$command = readline("Enter Command: ");
/* Then add the input to the command history */
readline_add_history($command);

If available, it really does make it simple.


Here a typical do-case-while for console implementation:

do {
  $cmd = trim(strtolower( readline("\n> Command: ") ));
  readline_add_history($cmd);
  switch ($cmd) {
    case 'hello': print "\n -- HELLO!\n"; break;
    case 'bye': break;
    default: print "\n -- You say '$cmd'... say 'bye' or 'hello'.\n";
  }
} while ($cmd!='bye');

where user can use arrows (up and down) to access the history.

查看更多
干净又极端
5楼-- · 2020-02-17 06:18

My five cents: Using STDOUT and STDIN:

fwrite(STDOUT, "Please enter your Message (enter 'quit' to leave):\n");

do{
    do{
        $message = trim(fgets(STDIN));
    } while($message == '');

    if(strcasecmp($message, 'quit') != 0){
        fwrite(STDOUT, "Your message: ".$message."\n");
    }

}while(strcasecmp($message,'quit') != 0);
// Exit correctly
exit(0);
查看更多
甜甜的少女心
6楼-- · 2020-02-17 06:22

From PHP: Read from Keyboard – Get User Input from Keyboard Console by Typing:

You need a special file: php://stdin which stands for the standard input.

print "Type your message. Type '.' on a line by itself when you're done.\n";

$fp = fopen('php://stdin', 'r');
$last_line = false;
$message = '';
while (!$last_line) {
    $next_line = fgets($fp, 1024); // read the special file to get the user input from keyboard
    if (".\n" == $next_line) {
      $last_line = true;
    } else {
      $message .= $next_line;
    }
}
查看更多
Evening l夕情丶
7楼-- · 2020-02-17 06:22

I found an example on PHP.net, Utiliser PHP en ligne de commande:

$handle = fopen("php://stdin", "r");
$line = fgets($handle);
if (trim($line) != 'yes') {
...
查看更多
登录 后发表回答