PHP wait for input from command line

PhpCommand Line

Php Problem Overview


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

  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

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

Php Solutions


Solution 1 - Php

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

<?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";
?>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1544337View Question on Stackoverflow
Solution 1 - PhpCrispView Answer on Stackoverflow