How to make one PHP script run another and write its output?

It's a little complicated, so bear with me:

  • I have a PHP script a.php that runs from the command line and the data is provided to it via STDIN
  • I have another php script b.php
  • I want to run a.php b.php and write its output.
  • In addition, a.php should redirect STDIN to b.php

Is there an easy way to do this?

+4
source share
2 answers

To just write the stdout of another program (php or not), you can use backlinks: http://php.net/manual/en/language.operators.execution.php . For instance:

 $boutput = `php b.php`; 

To write stdin, do the following:

 $ainput = file_get_contents('php://stdin'); 

Finally, to pass the contents of the string to an external program, use proc_open , as suggested by jeremy. In particular, here is what your a.php should contain:

 $ainput = file_get_contents('php://stdin'); $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w") ); $process = proc_open('php b.php', $descriptorspec, $pipes); fwrite($pipes[0], $ainput); fclose($pipes[0]); echo stream_get_contents($pipes[1]); # echo output of "b.php < stdin-from-a.php" fclose($pipes[1]); proc_close($process); 
+3
source

proc_open() should give you the level of control you need to do.

+1
source

Source: https://habr.com/ru/post/1307211/


All Articles