Search code examples
phpcommand-linepipe

piping data into command line php?


It is possible to pipe data using unix pipes into a command-line php script? I've tried

$> data | php script.php

But the expected data did not show up in $argv. Is there a way to do this?


Solution

  • As I understand it, $argv will show the arguments of the program, in other words:

    php script.php arg1 arg2 arg3
    

    But if you pipe data into PHP, you will have to read it from standard input. I've never tried this, but I think it's something like this:

    $fp = readfile("php://stdin");
    // read $fp as if it were a file