PHP 等待来自命令行的输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15322371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:03:06  来源:igfitidea点击:

PHP wait for input from command line

phpcommand-line

提问by Crisp

I want to be able to let a PHP program wait for user's input. For example:

我希望能够让 PHP 程序等待用户的输入。例如:

  1. Script requests authorization code from server (this code will be sent via e-mail)
  2. Script waits for user to enter authorization code
  3. Script continues
  1. 脚本向服务器请求授权码(此码将通过电子邮件发送)
  2. 脚本等待用户输入授权码
  3. 脚本继续

How should I do step 2? (Is it possible?)

我应该怎么做第 2 步?(是否可以?)

回答by Crisp

The php man page for the cli has a comment heredetailing a solution, (copied here for anyone else looking)

cli 的 php 手册页在这里有一条评论,详细说明了一个解决方案,(复制到这里供其他人查看)

<?php
echo "Are you sure you want to do this?  Type 'yes' to continue: ";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
if(trim($line) != 'yes'){
    echo "ABORTING!\n";
    exit;
}
fclose($handle);
echo "\n"; 
echo "Thank you, continuing...\n";
?>