Re: stdout as input

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



bdunlap wrote:
>> 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

Exactly.  And if you just want redirected data you can try:

$data = file_get_contents("php://stdin");

--or--

For an array of lines:

$lines = file("php://stdin");

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux