Supported Versions: PHP 4, PHP 5, PHP 7, PHP 8
Binary-safe file write
<?php fwrite(resource $stream, string $data, ?int $length = null): int|false
<?php require __DIR__ . '/../vendor/autoload.php';
if (!defined('SIGINT')) {
fwrite(STDERR, 'Not supported on your platform (ext-pcntl missing or Windows?)' . PHP_EOL);
exit(1);
}
<?php require __DIR__ . '/../vendor/autoload.php';
if (!defined('STDIN') || stream_set_blocking(STDIN, false) !== true) {
fwrite(STDERR, 'ERROR: Unable to set STDIN non-blocking (not CLI or Windows?)' . PHP_EOL);
exit(1);
}
<?php $data = str_repeat($data, round(200000 / strlen($data)));
if (!defined('STDOUT') || stream_set_blocking(STDOUT, false) !== true) {
fwrite(STDERR, 'ERROR: Unable to set STDOUT non-blocking (not CLI or Windows?)' . PHP_EOL);
exit(1);
}
<?php Loop::addWriteStream(STDOUT, function ($stdout) use (&$data) {
$r = fwrite($stdout, $data);
if ($r === 0) {
<?php Loop::removeWriteStream($stdout);
fclose($stdout);
stream_set_blocking($stdout, true);
fwrite(STDERR, 'Stopped because STDOUT closed' . PHP_EOL);
return;
}
<?php stream_set_blocking($stream, false);
fwrite($stream, "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n");
Loop::addReadStream($stream, function ($stream) {
<?php }
fwrite($stream, "GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n");
Loop::addReadStream($stream, function ($stream) {
<?php $conn = stream_socket_accept($server);
$data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";
Loop::addWriteStream($conn, function ($conn) use (&$data) {
$written = fwrite($conn, $data);
if ($written === strlen($data)) {
fclose($conn);
Loop::removeWriteStream($conn);
<?php $loop->cancelTimer($timeout);
});
fwrite($output, "foo\n");
$this->loop->run();
<?php $this->loop->addReadStream($input, $this->expectCallableExactly(2));
fwrite($output, "foo\n");
$this->tickLoop($this->loop);
fwrite($output, "bar\n");
<?php fwrite($output, "foo\n");
$this->tickLoop($this->loop);
fwrite($output, "bar\n");
$this->tickLoop($this->loop);
}
<?php $this->loop->addReadStream($input, $this->expectCallableExactly(2));
$this->loop->addReadStream($input, $this->expectCallableNever());
fwrite($output, "foo\n");
$this->tickLoop($this->loop);
fwrite($output, "bar\n");
<?php fwrite($output, "foo\n");
$this->tickLoop($this->loop);
fwrite($output, "bar\n");
$this->tickLoop($this->loop);
}
<?php {
list ($input, $output) = $this->createSocketPair();
fwrite($input, 'hello');
fclose($input);
$loop = $this->loop;
<?php $this->loop->addReadStream($input, $this->expectCallableNever());
$this->loop->removeReadStream($input);
fwrite($output, "bar\n");
$this->tickLoop($this->loop);
}