PHP reads what comes from the shell pipe

  • 2020-03-31 20:24:44
  • OfStack

Evening summer, how do you do?
Rainbird sent you a short message:
I have written a lot of deamon running in the background. It is quite handy to use, but now I want to get the content sent by the pipeline, I don't know how to implement, similar to echo "aaaa" | a.hp, how to get the content of echo by a.hp, I don't know what you have to say.
Received a message today, the solution to share with you:
In fact, the shell's | actually represents the standard output of the previous as the standard input of the latter. Although the implementation is through pipe,
But you don't need to know what's going on when you write PHP code. You can read it directly as standard input:
Here is an experiment code:
 
<?php 
$fp = fopen("php://stdin", "r"); 
$s = ''; 
while (!feof($fp)) 
{ 
$s .= fgets($fp, 128); 
} 
var_dump($s); 
fclose($fp); 
?> 

Test method:
 
ls -lh | php php_read_pipe.php 

Rainbird also offers simpler code:
File_get_contents (' PHP: / / stdin)
If you have a lot of data to transmit, it's typically every 4K.
Until the transmission is complete. That may not be easy to use:
File_get_contents (' PHP: / / stdin). That way, you'll be waiting.
Separately, you can read a certain amount and then process a certain amount. Then free up some memory.
Let's say I want to go through all the files. You can do this
Find / | PHP PHP php_read_pipe. PHP
We are specific to the specific situation analysis.

Related articles: