Home  >  Article  >  Backend Development  >  PHP reads the content transmitted by the shell pipe_PHP tutorial

PHP reads the content transmitted by the shell pipe_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:40:06965browse

Hello, Mu Xia!
Rainbird sent you a short message:
I have written a lot of background running deamon. It is very easy to use, but now I want to get the content passed through the pipe, and I don’t know how to achieve it, similar to echo " aaaa" |a.php, a.php how to get the echo content, do you have any advice?
I received a message today, and I would like to share the solution with you:
Actually, the shell's | The above indicates that the standard output of the previous one is used as the standard input of the next one. Although the implementation is implemented through pipe,
you do not need to know the underlying operation when writing PHP code. You can just read it directly as standard input:
The following is an experimental code:

Copy the code The code is as follows:

$fp = fopen("php://stdin", "r");
$s = '';
while (!feof($fp))
{
$s .= fgets($fp, 128);
}
var_dump($s);
fclose($fp);
?>

Test method:
Copy code The code is as follows:

ls -lh | php php_read_pipe.php

Rainbird also gives a simpler code:
file_get_contents('php://stdin')
If there is a lot of data to be transferred, generally speaking, it is transferred once every 4K.
Until the transfer is completed. That might not be possible simply using:
file_get_contents('php://stdin'). If so, I will always be waiting.
If you process them separately, you can read a certain amount and then process part of them. Then release part of the memory.
For example, I want to traverse all files. It can be handled like this
find / | php php php_read_pipe.php
We will analyze it based on the specific situation.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321414.htmlTechArticleHello Mu Xia! Rainbird sent you a short message: I have written a lot of background running deamon. It is very easy to use, but now I want to get the content passed through the pipe, and I don’t know how to achieve it...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn