Re: stdout as input
| From: | bdunlap | Date: | Thu, 24 Sep 2009 16:00:27 +0000 |
| Subject: | Re: stdout as input | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-298320@lists.php.net to get a copy of this message | ||
> echo bob | myScript.php
>
> In myScript.php, I'm doing:
>
> $handle = popen( 'php://stdin', 'r' );
> echo var_export( $handle, TRUE ) . "\n\n";
What output are you getting from those lines, and what were you
expecting? $handle is just a resource, same as if you opened a regular
file -- you still need to read data from it with something like
fgets().
If I'm writing a quick-and-dirty script to process piped-in data, I
find this construct to work reasonably well:
while(!feof(STDIN)) {
$line = fgets(STDIN);
// trim() $line here if I don't want trailing/vertical whitespace
// now do something with $line
}
The one weakness of that form is that I always get an empty line at
the end, but as long as I check for that in the loop (which I should
usually be doing anyway), it's no problem.
Ben