php脚本获取管道的输入数据

 

cat get_pipedata.php

#!/usr/bin/env php
<?php
#========================================================
#testing how to get data from pipe
#========================================================
$fp=fopen('php://stdin','r');
$result='';
while(!feof($fp)){
    $result.=fgets($fp,128);
}
fclose($fp);

echo $result;

新版:

#!/usr/bin/env php
<?php
$fp=fopen('php://stdin','r');
$content='';
if($fp){
        while($line=fgets($fp,4096)){
                echo $line.PHP_EOL;
        }
        fclose($fp);
}

 

测试:

echo 'test message'|get_pipedata.php

tail -f -n +1 /var/log/user.log|get_pipedata.php

posted @ 2012-08-14 10:46  涛光  阅读(2206)  评论(0编辑  收藏  举报