3

I am somewhat new to php, but I do understand some of the key concepts of it, however nothing about STDIN, what it is and why I am getting this error.

I am developing with the eTrade API PHP SDK and the development community over there seems to be a ghost-town, and absolutely nothing here on stackoverflow.

Here is the result of the php script I am trying to run http://philiparudy.com/etrade/Samples/test_market.php

I guess I am confused about this simple sample script that they give you where the end of the php script looks like this:

function get_input($str)
{
 echo "\nPlease enter * $str * : ";
 return trim(fgets(STDIN));
}
function show_menu()
{
 echo "\n\nChoose from following options..\n\n";
 echo "1. Get Option Chain\n\n";
 echo "2. Product Lookup\n\n";
 echo "3. Get Expiry Dates\n";
 echo "4. Get Quote\n";
 echo "0. Exit\n";
 echo "Enter your choice:";
 $choice = trim(fgets(STDIN));
return $choice;
}

Above all of this is a switch function that is supposed to handle your input, but there is no place to put the input on the webpage?

I also tried copying and pasting this in the terminal to which I got an array of different errors. Am I missing the boat completely here?

Bruce
  • 107
  • 1
  • 3
  • 8
  • Using [`file_get_contents()`](http://php.net/manual/en/function.file-get-contents.php) may be simpler. Least, that's what I found in a few threads. – Funk Forty Niner Oct 01 '14 at 19:19
  • I'm not really sure what you're trying to do here. If you want to fetch input from a web page, you need to create an HTML form that sends data to your PHP script via the `$_GET` or `$_POST` arrays. You won't get very far with `fgets(STDIN)` — this will simply wait for a line of text to be entered from the keyboard (i.e., the one connected to your web server, which may not even exist) – r3mainer Oct 01 '14 at 19:43
  • Not all PHP development targets the web, you can also use it as general purpose programming language, @squeamishossifrage. – Ulrich Eckhardt Oct 01 '14 at 20:12
  • According to http://php.net/manual/en/features.commandline.io-streams.php, your code should work. The thing that is assumed on the website is that you are running this from the commandline (CLI means CommandLine Interface) and not from a webserver. Note also that if this is your whole script, you will need ` – Ulrich Eckhardt Oct 01 '14 at 20:17
  • @UlrichEckhardt I realise that, but if you read the question you'll see that the OP is — apparently — trying to publish a command line application on the web and is having difficulties because "there is no place to put the input on the webpage". – r3mainer Oct 01 '14 at 21:20
  • Yes i figure it out, this needs to be run in the command line. I am now trying to figure out if there is a way to build a REST request instead of doing this through the CLI – Bruce Oct 01 '14 at 21:53
  • yes, how would you build a REST? – Bruce Oct 08 '14 at 16:50

1 Answers1

2

i found this:

$stdin = fopen('php://stdin', 'r');
$response = fgetc($stdin);

fopen() opens a stream of stdin as readonly ('r') and fgetc() waits for the input (only one caracter and you have to hit enter). with fgets() you can read a whole line (the lineending is an EOL (\n, \r or so)) (that's only for command line use else it wont work)

Thorbijoern
  • 356
  • 2
  • 11