Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: fgets() expects parameter 1 to be resource, string given in

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?

like image 847
Bruce Avatar asked Jan 29 '26 03:01

Bruce


1 Answers

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)

like image 121
Thorbijoern Avatar answered Jan 31 '26 15:01

Thorbijoern



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!