1. Go to this page and download the library: Download takuya/php-process-exec library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
takuya / php-process-exec example snippets
// prepare
$p1arg = new ExecArgStruct("cat /etc/passwd ");
$p2arg = new ExecArgStruct('grep takuya');
// pipe
$p1 = new ProcessExecutor($p1arg);
$p2 = $p1->pipe($p2arg);
$p2->start();
echo $p2->getOutput();
$executor = new ProcessExecutor(['bash']);
$executor->setInput('
for i in {0..4}; do
echo $i
done;
');
$executor->start();
//blocking io
echo $executor->getOutput();
$arg = new ExecArgStruct('php');
$src =<<<'EOS'
foreach(range(0,4) as $i){
printf("%d\n",$i);
}
EOS;
$arg->setInput( $src );
$executor = new ProcessExecutor( $arg );
$executor->onStdOut(function ($line){ //=> each line.
echo $line.PHP_EOL;
});
$executor->start();
// process argument as class
$arg = new ExecArgStruct();
$arg->setCmd( ['php'] );
$src = <<<'EOS'
echo 'Hello World';
EOS;
$arg->setInput( $src );
// run callback by running status.
$observer = new ProcessObserver();
$observer->addEventListener( ProcessErrorOccurred::class, fn()=> fwrite("php://stderr","エラー") );
$observer->addEventListener( ProcessFinished::class, fn($ev) =>print($ev->getExecutor()->getOutput()) );
// start process.
$executor = new ProcessExecutor( $arg );
$executor->addObserver( $observer );
$executor->start();
# avoid Shell Injection vulnerability by pass as array
# Skip shell escaping.
$file_name = 'my long spaced doc.txt';
proc_open(['cat',$file_name]...);
## using proc_open as Class
$proc = new ProcOpen(['cat',$fname]);
$proc->start();
echo stream_get_contents($proc->stdout());
## command option as struct
$struct = new ExecArgStruct(['cat',$fname]);
$proc = new ProcessExecutor($struct);
$proc->start();
echo $proc->getOutput();
// reusable ARGS and easy to run multiple times.
$proc = new ProcessExecutor($struct);
$proc->start();
echo $proc->getOutput();
class RestrictedArg extends ExecArgStruct {
public function __construct(...){
// check allow command.
$this->check() || throw new InvalidArgumentException();
}
}
// InvalidArgumentException
$struct = new RestrictedArg(['passwd',$name]);
class MyFFmpegStruct extends ExecArgStruct {
public function __construct(...){
// check command options.
$this->checkOptions() || throw new \Exception('you need "-f" option ');
}
}
//-> InvalidArgumentException
$struct = new MyFFmpegStruct(['ffmpeg','-i',$name]);
// define Struct of CMD
$arg = new ExecArgStruct('php -i');
$executor = new ProcessExecutor($arg);
// Observer( aggregate of Listener ).
$observer = new ProcessObserver();
$observer->addEventListener(ProcessStarted::class, fn()=>dump('started')));
$observer->addEventListener(ProcessSuccess::class, fn()=>dump('successfully finisihed')));
$observer->addEventListener(ProcessRunning::class, fn()=>dump('running')));
$observer->addEventListener(ProcessRunning::class, function(ProcessRunning $ev){
// Listeners can be added per Event.
printf("pid=%d",$ev->getExecutor()->getProcess()->info->pid);
});
// Second Observer.
$streamObs = new ProcessObserver();
$streamObs->addEventListener(StdoutChanged::class, fn()=>dump('stdout changed')));
// Bind Observer with CMD executor
$executor->addObserver($observer);
$executor->addObserver($streamObs);
$executor->start();
## simple listener.
$executor = new ProcessExecutor( new ExecArgStruct(['cat','file']) );
$executor->onStdOut(function ($line){
echo $line.PHP_EOL;
});
$executor->start();
$executor = new ProcessExecutor( new ExecArgStruct(['cat','-']) );
$executor->setInput(fopen('file','r'));
$executor->onInputProgress(fn($percent)=>printf("%s%%\n",$percent));
$executor->start();