Swoole Hook Stream Function

The SWOOLE_HOOK_STREAM_FUNCTION flag will enable coroutine support for the PHP stream_select() function. Support was added in v4.4.0.

Example

<?php

Co::set(['hook_flags' => SWOOLE_HOOK_STREAM_FUNCTION]);

Co\run(function() 
{
    $fp1 = stream_socket_client("tcp://www.baidu.com:80", $errno, $errstr, 30);
    $fp2 = stream_socket_client("tcp://www.qq.com:80", $errno, $errstr, 30);

    if(!$fp1) 
    {
        echo "$errstr ($errno) \n";
    } 
    else
    {
        fwrite($fp1, "GET / HTTP/1.0\r\nHost: www.baidu.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);
    }
});