1. Go to this page and download the library: Download react/child-process 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/ */
react / child-process example snippets
$process = new React\ChildProcess\Process('echo foo');
$process->start();
$process->stdout->on('data', function ($chunk) {
echo $chunk;
});
$process->on('exit', function($exitCode, $termSignal) {
echo 'Process exited with code ' . $exitCode . PHP_EOL;
});
$process = new Process($command);
$process->start();
$process->stdout->on('data', function ($chunk) {
echo $chunk;
});
$process->stdout->on('end', function () {
echo 'ended';
});
$process->stdout->on('error', function (Exception $e) {
echo 'error: ' . $e->getMessage();
});
$process->stdout->on('close', function () {
echo 'closed';
});
$process->stdin->write($data);
$process->stdin->end($data = null);
// …
$process = new Process('echo test');
$process->start();
$process = new Process('ping example.com', null, null, array());
$process->start();
$process->on('exit', function ($exitcode) {
echo 'exit with ' . $exitcode . PHP_EOL;
});
$process = new Process('ping example.com', null, null, array(
array('file', 'nul', 'r'),
$stdout = tmpfile(),
array('file', 'nul', 'w')
));
$process->start();
$process->on('exit', function ($exitcode) use ($stdout) {
echo 'exit with ' . $exitcode . PHP_EOL;
// rewind to start and then read full file (demo only, this is blocking).
// reading from shared file is only safe if you have some synchronization in place
// or after the child process has terminated.
rewind($stdout);
echo stream_get_contents($stdout);
fclose($stdout);
});
$server = new React\Socket\Server('127.0.0.1:0');
$server->on('connection', function (React\Socket\ConnectionInterface $connection) {
$connection->on('data', function ($chunk) {
echo $chunk;
});
});
$command = 'ping example.com | foobar ' . escapeshellarg($server->getAddress());
$process = new Process($command, null, null, array());
$process->start();
$process->on('exit', function ($exitcode) use ($server) {
$server->close();
echo 'exit with ' . $exitcode . PHP_EOL;
});
$socket = stream_socket_client($argv[1]);
do {
fwrite($socket, $data = fread(STDIN, 8192));
} while (isset($data[0]));