【问题标题】:How do I write a command-line interactive PHP script?如何编写命令行交互式 PHP 脚本?
【发布时间】:2011-02-25 03:45:16
【问题描述】:

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

提示和接受重复的命令行输入是让我感到困惑的部分。我该怎么做?

【问题讨论】:

    标签: php shell


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          【解决方案4】:

          算法很简单:

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

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

          【讨论】:

          • “读取输入的命令行”部分让我大吃一惊。你如何让它不断地轮询用户输入?
          • 通常它会像 while ($line = readline($promptstring));细节基本上相当于您想要的错误检查类型,以及用户要求退出的方式。看看手册中的 fgets() 和 readline() 函数。
          【解决方案5】:

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

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

          【讨论】:

            【解决方案6】:

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

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

            使用起来很简单:

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

            If available,确实很简单。


            这里是控制台实现的典型 do-case-while:

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

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

            【讨论】:

            【解决方案7】:

            一行代码(第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?

            【讨论】:

              【解决方案8】:

              简单:

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

              【讨论】:

                【解决方案9】:

                我的五分钱: 使用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);
                

                【讨论】:

                  猜你喜欢
                  • 2012-07-03
                  • 2012-02-20
                  • 2014-11-24
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-12-26
                  • 1970-01-01
                  相关资源
                  最近更新 更多