Supported Versions: PHP 4, PHP 5, PHP 7, PHP 8
Binary-safe file write
Alias fputs
fwrite(resource $streamstring $data, ?int $length null): int|false
require __DIR__ '/../vendor/autoload.php';



if (!
defined('SIGINT')) {

    
fwrite(STDERR'Not supported on your platform (ext-pcntl missing or Windows?)' PHP_EOL);

    exit(
1);

}
require __DIR__ '/../vendor/autoload.php';



if (!
defined('STDIN') || stream_set_blocking(STDINfalse) !== true) {

    
fwrite(STDERR'ERROR: Unable to set STDIN non-blocking (not CLI or Windows?)' PHP_EOL);

    exit(
1);

}
$data str_repeat($dataround(200000 strlen($data)));



if (!
defined('STDOUT') || stream_set_blocking(STDOUTfalse) !== true) {

    
fwrite(STDERR'ERROR: Unable to set STDOUT non-blocking (not CLI or Windows?)' PHP_EOL);

    exit(
1);

}
Loop::addWriteStream(STDOUT, function ($stdout) use (&$data) {


    
$r fwrite($stdout$data);




    if (
$r === 0) {
        Loop::removeWriteStream($stdout);

        
fclose($stdout);

        
stream_set_blocking($stdouttrue);

        
fwrite(STDERR'Stopped because STDOUT closed' PHP_EOL);



        return;

    }
stream_set_blocking($streamfalse);




fwrite($stream"GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n");




Loop::addReadStream($stream, function ($stream) {
    }




    
fwrite($stream"GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: close\r\n\r\n");




    
Loop::addReadStream($stream, function ($stream) {
    $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);
            $loop->cancelTimer($timeout);

        });



        
fwrite($output"foo\n");



        
$this->loop->run();
        $this->loop->addReadStream($input$this->expectCallableExactly(2));



        
fwrite($output"foo\n");

        
$this->tickLoop($this->loop);



        
fwrite($output"bar\n");
        fwrite($output"foo\n");

        
$this->tickLoop($this->loop);



        
fwrite($output"bar\n");

        
$this->tickLoop($this->loop);

    }
        $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");
        fwrite($output"foo\n");

        
$this->tickLoop($this->loop);



        
fwrite($output"bar\n");

        
$this->tickLoop($this->loop);

    }
    {

        list (
$input$output) = $this->createSocketPair();



        
fwrite($input'hello');

        
fclose($input);



        
$loop $this->loop;
        $this->loop->addReadStream($input$this->expectCallableNever());

        
$this->loop->removeReadStream($input);



        
fwrite($output"bar\n");

        
$this->tickLoop($this->loop);

    }