Re: [PHP] stdout as input
> 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"); This is exactly what I was looking for. Thanks Shawn and Ben! thnx, Christoph -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] stdout as input
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
Re: [PHP] stdout as input
> 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] stdout as input
> Isn't it simpler with > http://www.php.net/manual/en/function.shell-exec.php > ? Perhaps. But the command to execute would be relatively arbitrary. What I'm trying to do is set up a script that will filter the results of a grep. But what I'm grepping and where I'm starting from would change. I suppose I could pass those arguments to my script and access it using argv but I'm actually kind of curious how (if) I can access output which has been piped (or redirected) to my script. thnx, Christoph -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] stdout as input
- Original Message > From: Christoph Boget > To: PHP General > Sent: Thursday, September 24, 2009 6:51:56 AM > Subject: [PHP] stdout as input > > I've read the section in the docs about i/o streams and other related > sections I was able to find but couldn't figure out how to do what I > need. From the command line, I'm trying to pipe stdout in to my php > script which should then see it as input. A simplistic example would > be: > > echo bob | myScript.php > > In myScript.php, I'm doing: > > $handle = popen( 'php://stdin', 'r' ); > echo var_export( $handle, TRUE ) . "\n\n"; > > I've also tried php://stdout and php://input. I also tried eschewing > the php:// pseudo protocol and popen altogether and going with the > STDIN and STDOUT constants. None of that worked. Is there a way I > can pipe/redirect output from a command line uhh, command as input to > my script? > > thnx, > Christoph > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php Isn't it simpler with http://www.php.net/manual/en/function.shell-exec.php ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php