OpenSwoole Hook SSL

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

The OpenSwoole\Runtime::HOOK_SSL flag will enable coroutine support for SSL sockets and streams. Support was added in OpenSwoole v4.2.0.

Example

<?php

Co::set(['hook_flags' => OpenSwoole\Runtime::HOOK_SSL]);

Co::run(function()
{
    $host = 'host.domain.tld';
    $port = 1234;
    $timeout = 12;
    $cert = '/path/to/your/certchain/certchain.pem';

    $context = stream_context_create(
        array(
            'ssl' => array(
                'local_cert' => $cert,
            )
        )
    );

    if($fp = stream_socket_client(
        'ssl://' . $host . ':' . $port,
        $errno,
        $errstr,
        30,
        STREAM_CLIENT_CONNECT,
        $context
    ))
    {
        echo "connected\n";
    }
    else
    {
        echo "ERROR: $errno - $errstr \n";
    }
});

Peer verification

Since v26.11.0, hooked ssl:// and tls:// client streams verify the peer like native PHP:

  • verify_peer is true by default, the server certificate must be valid
  • verify_peer_name is true by default, the certificate must match the host name
  • allow_self_signed is false by default
  • The TLS host name (SNI and the name to verify) is taken from the host in the address, set peer_name to use another name

Earlier versions did not verify the peer unless you set these options. To connect to a server with a self-signed certificate, turn the checks off in the stream context:

<?php

Co::set(['hook_flags' => OpenSwoole\Runtime::HOOK_ALL]);

Co::run(function()
{
    $context = stream_context_create([
        'ssl' => [
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],
    ]);

    $fp = stream_socket_client('ssl://127.0.0.1:9501', $errno, $errstr, 5, STREAM_CLIENT_CONNECT, $context);
});

Or keep verification on and trust your own CA with cafile or capath.

The ssl context options read by the hook are peer_name, verify_peer, verify_peer_name, allow_self_signed, cafile, capath, local_cert, local_pk, passphrase, verify_depth and disable_compression.

Last updated on November 17, 2026