PHP管道与读取进程数据

windows的cmd也能用。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
#!/usr/local/bin/php -q
function read(){
    $fp =  fopen("php://stdin""r");
    while(!feof($fp)) {
        $tmp fgets($fp, 255);
        $input $input$tmp;
    }
         
    fclose($fp);
    return $input;
}
 
 
$input = read();
echo $input;

php处理文件流

cat a.txt |/usr/local/bin/php in.php 

 

echo.php

<?php

ob_implicit_flush(true);

$i = 0;
while(1){
   echo "echo {$i}\n";

   sleep(1);
   if($i> 2)exit();

   $i++;
}

  

read.php

<?php
/*
function read(){
    $fp =  fopen("php://stdin", "r");
    while(!feof($fp)) {
        $tmp = fgets($fp, 255);
        $input = $input. $tmp;
    }
         
    fclose($fp);
    return $input;
}
 
 
$input = read();
var_dump($input);
*/

while($stdin = fread(STDIN, 65535)){
    var_dump($stdin);
    
}

  

可以进行实时的非阻塞读取数据输出,而不是阻塞的读取

 php echo.php | php read.php

 

posted @ 2017-11-15 09:49  luckc#  阅读(835)  评论(0)    收藏  举报