如何编写命令行交互式PHP脚本?

时间:2010-05-28 14:00:29

标签: php shell

我想编写一个可以在命令行中使用的PHP脚本。我希望它提示并接受一些项目的输入,然后吐出一些结果。我想在PHP中执行此操作,因为我的所有类和库都是用PHP编写的,我只想创建一个简单的命令行界面。

提示和接受重复的命令行输入是绊倒我的部分。我该怎么做?

9 个答案:

答案 0 :(得分:18)

PHP手册中的I/O Streams页面描述了如何使用STDIN从命令行读取一行:

<?php
 $line = trim(fgets(STDIN)); // reads one line from STDIN
 fscanf(STDIN, "%d\n", $number); // reads number from STDIN
?>

答案 1 :(得分:13)

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

  

您需要一个特殊文件: php://stdin ,代表标准输入。

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;
    }
}

答案 2 :(得分:9)

我不确定您的输入有多复杂,但readline是处理交互式CLI程序的绝佳方式。

您可以获得与shell相同的生物舒适度,例如命令历史记录。

使用它就像:

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

If available,确实让它变得简单。


这是控制台实现的典型案例:

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');

用户可以使用箭头(向上和向下)访问历史记录。

答案 3 :(得分:3)

我在PHP.net上找到了一个例子, Utiliser PHP en ligne de commande

$handle = fopen("php://stdin", "r");
$line = fgets($handle);
if (trim($line) != 'yes') {
...

答案 4 :(得分:1)

算法很简单:

until done:
    display prompt
    line := read a command line of input
    handle line

使用将命令映射到处理它们的回调函数的数组非常简单。整个挑战大致是while循环和两个函数调用。 PHP还有一个readline interface用于更高级的shell应用程序。

答案 5 :(得分:1)

一行代码(第2行):

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

在博客文章 How can I capture user input from the cmd line using PHP? 中查看此答案的来源。

答案 6 :(得分:1)

简单:

#!/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";

答案 7 :(得分:1)

我的五美分: 使用STDOUTSTDIN

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);

答案 8 :(得分:0)

基本上你从标准输入读取。请参阅 Input/output streams