How to process stdin to stdout in php?

天涯浪子 提交于 2019-12-06 04:13:00

问题


I'm trying to write a simple php script to take in data from stdin, process it, then write it to stdout. I know that PHP is probably not the best language for this kind of thing, but there is existing functionality that I need.

I've tried

<?php
$file = file_get_contents("php://stdin", "r");
echo $file;
?>

but it doesn't work. I'm invoking it like this: echo -e "\ndata\n" | php script.php | cat. and get no error messages. The script I'm trying to build will actually be part of a larger pipeline.

Any clues as to why this is not working?

PS: I'm not very experienced with PHP.


回答1:


If you are piping, you will want to buffer the input, instead of processing it all at once, just go one line at a time as is standard for *nix tools.

The SheBang on top of the file allows you to execute the file directly, instead of having to call php in the command line.

Save the following to test.php and run

cat test.php | ./test.php

to see the results.

#!php
<?php
$handle = fopen('php://stdin', 'r');
$count = 0;
while(!feof($handle)) {
    $buffer = fgets($handle);
    echo $count++, ": ", $buffer;
}
fclose($handle);



回答2:


To place a php-script in a pipe you can use:

xargs -d "\n" ./mysrcipt.php --foo

With many lines/args the ./myscript.php will be called a couple times, but always with --foo.

e.g.:

./myscript.php:
#!/bin/php
<?php
foreach($args as $key => $value){
 echo "\n".$key.":".$value;
}
?>
cat -n1000 /path/file | xargs -d "\n" ./myscript.php --foo | less

will call the script two times with echo to stdout/less:

0:./myscript
1:--foo
2:[file-line1]
3:[file-line2]
...
800:[file-line799]
0:./myscript
1:--foo
2:[file-line800]
...

source




回答3:


Right, got it working.

<?php
  $input_stream = fopen("php://stdin","r");
  $text="";
  while($line = fgets($input_stream,4096)){ // Note 4k lines, should be ok for most purposes
    $text .= $line;
  }

  fclose($input_stream);
  print($text);
?>

from a recipe at PHPBuilder.



来源:https://stackoverflow.com/questions/3249427/how-to-process-stdin-to-stdout-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!