PHP code example of takuya / php-proc_open-wrapper
1. Go to this page and download the library: Download takuya/php-proc_open-wrapper 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-proc_open-wrapper example snippets
akuya\ProcOpen\ProcOpen;
$p = new ProcOpen( ['php','-v'] );
$p->start();
// This is enough to wait process end, because blocked.
echo $output = stream_get_contents($p->getFd(1));
akuya\ProcOpen\ProcOpen;
$p = new ProcOpen( ['sleep','30'] );
$p->start();
$p->wait();
$proc = new ProcOpen(['bash'],__DIR__,['SHELL'=>'php']);
$proc->setInput('
for i in {0..4}; do
echo $i
done;
');
$proc->start();
$proc->wait();
//blocking io
echo fread($proc->getFd(1), 1024);
$proc = new ProcOpen(['python']);
$proc->setInput('
import os;
import sys
for i in range(5):
print(i);
print("end")
print(sys.path)
');
$proc->start();
$proc->wait();
//blocking io
echo stream_get_contents($proc->getFd(ProcOpen::STDOUT);
$proc = new ProcOpen(['cat','/etc/passwd']);
$proc->start();
// $proc automatically wait by OS blocking io.
// `$proc->wait();` is not necessary.
echo stream_get_contents($proc->stdout);
// a shortcut function.
$proc->getOutput();
// Same to this.
stream_get_contents($proc->stdout)
$proc = new ProcOpen( ['php'] );
$proc->setInput(<<<'EOS'
define('LINUX_PIPE_SIZE_MAX',1024*64+1);
foreach(range(1,LINUX_PIPE_SIZE_MAX) as $i){
echo 'a';
}
EOS);
$proc->start();
// this will be blocked.
$popen = new ProcOpen(['php','-i'],null,$env);
$popen->start();
// instead of wait() use blockingIO.
return stream_get_contents($popen->stdout());
$proc = new ProcOpen( ['php'] );
$proc->setInput(<<<'EOS'
define('LINUX_PIPE_SIZE_MAX',1024*64+1);
foreach(range(1,LINUX_PIPE_SIZE_MAX) as $i){
echo 'a';
}
EOS);
$proc->setStdout($tmp = fopen('php://temp','w'));
$proc->start();
// this will be successfully finished.
$proc = new ProcOpen( ['php'] );
$proc->setInput(<<<'EOS'
define('LINUX_PIPE_SIZE_MAX',1024*64+1);
foreach(range(1,LINUX_PIPE_SIZE_MAX) as $i){
echo 'a';
}
EOS);
$proc->start();
// use select not to be clogged.
$output = '';
$avail = stream_select( ...( $selected = [[$proc->getFd( 1 )], [], null, 0, 100] ) );
if ( $avail > 0 ) {
$output .= fread( $proc->getFd( 1 ), 1 );
}
$popen = new ProcOpen(['php','-i'],null,$env);
// This is not memory , proc_open cast IO to /tmp/phpXXXX .
$popen->setOutput($out=fopen('php://temp/maxmemory:'.(1024*1024*10)));
$popen->start();
$popen->wait();
// in case Ctrl-C this will remain temp_file in /tmp
echo $popen->getOutput();//
$popen = new ProcOpen(['php','-i'],null,$env);
$popen->enableBuffering();//FLAG
// after add Flag , memory can be used.
$popen->setOutput(fopen('php://memory','w+'));
$popen->start();
$popen->wait();
echo $popen->getOutput();// no blocking, no error.