OpenSwoole Hook Stream Function

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

The `OpenSwoole\Runtime::HOOK_STREAM_FUNCTION flag will enable coroutine support for the PHP stream_select() function. Support was added in OpenSwoole v4.4.0.

Example

<?php

co::set(['hook_flags' => `OpenSwoole\Runtime::HOOK_STREAM_FUNCTION]);

co::run(function()
{
    $fp1 = stream_socket_client("tcp://www.google.com:80", $errno, $errstr, 30);
    $fp2 = stream_socket_client("tcp://www.facebook.com:80", $errno, $errstr, 30);

    if(!$fp1)
    {
        echo "$errstr ($errno) \n";
    }
    else
    {
        fwrite($fp1, "GET / HTTP/1.0\r\nHost: www.google.com\r\nUser-Agent: curl/7.58.0\r\nAccept: */*\r\n\r\n");

        $r_array = [$fp1, $fp2];
        $w_array = $e_array = null;
        $n = stream_select($r_array, $w_array, $e_array, 10);
        $html = '';

        while(!feof($fp1))
        {
            $html .= fgets($fp1, 1024);
        }

        fclose($fp1);
    }
});
Last updated on September 20, 2022