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/ */
$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 fread($proc->getFd(ProcOpen::STDOUT), 1024);
$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.
$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 );
}
composer
shell
php sample.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.