Hello,

I'm trying to use fgets() to get data from an RPG script via a Standard Input stream. I'm familiar w/ using fgets() like this and feof after every line:

$file = fopen("test.txt","r");

while(! feof($file))
  {
  echo fgets($file). "<br />";
  }

fclose($file);

My issue is, the RPG script is calling my PHP script and is sending me a data stream. Which is fine for 1 line of data but when there are multiple lines of data I'm not sure how to receive it or loop through it to setup my variables?

Here's how I handle one input string:

$cust = fgets(STDIN);

STDIN is the input I'm receiving. Would something like this work w/o a loop :

$cust = fgets(STDIN, 6);
$bizname = fgets(STDIN, 30);
$buildname = fgets(STDIN, 30);
$street = fgets(STDIN, 30);
$street2 = fgets(STDIN, 30);
$politicalDivision2 = fgets(STDIN, 17);
$politicalDivision1 = fgets(STDIN, 2);
$zip = fgets(STDIN, 5);
$ziplow = fgets(STDIN, 4);
$country = fgets(STDIN, 2);

where I include the length of the string or do I need to create some type of loop and add the data to an array? If so, how would I create a loop w/ only fgets? foreach, while?? I've kind of hit a road block.

Thanks in advance,
j

    I have something similar. It's been a long time since I've had to play with standard input, but it should adapt to your case.

    if( !defined( "STDIN" ) ) {
            define( "STDIN", fopen( 'php://stdin', 'r' ) );
    }
    
    
    while(( $t_line = fgets( STDIN, 1024 ) ) ) {
    //... process logic
    }
    

    Each iteration of the loop will have the next line of stdin, so you can add your processing logic in there as if you were dealing with just one line, and it will do it for each line passed.

    My brain is ready to quit on me (end of the day friday). I want to say the 1024 represented the max line length, but I can't be certain right now.

      Thanks for the reply Res - I will give that a shot. I have two questions though:

      What does this piece of code mean or what is it referring to?

      fopen( 'php://stdin', 'r' )//in particular -> "  'php://stdin'  "
      

      Is that just referring to itself or the actual standard input?

      Also, with that , do you think this would be possible:

      $input = fopen('php://stdin', 'r'); 
      
      while(! feof($input)) 
        { 
        $record[0] = fgets($input); 
        } 
      
      

      It will most likely be Monday before I test this out. I appreciate the insight.

        The fopen part creates a handler to the stdin, just like it would if it was a file, I really can't remember why I declared it as a constant, I'm sure it's not necessary, could have been done in a variable. I think our two snippets for processing the lines once you have the handler accomplish the same thing. You're looping until the file handler says you are at the end, my example loops until an attempt to get the next line fails, which is the end of the file. They should both work the same I think.

          5 days later

          Res - thanks for your help. That solution worked both as a constant and variable. It's always fun working w/ an RPG programmer and their programs - learning lots of new old stuff :queasy:

          j

            Write a Reply...