Swoole\Coroutine\Client->enableSSL()

Declaration

<?php
Swoole\Coroutine\Client->enableSSL(): bool

Parameters

None

Return

Returns true when successful and false when not.


Description

Enable SSL encryption dynamically.

Even though you can setup SSL connections right from the start of a client using the configuration, you can also use this method to enable SSL dynamically when you need to.

You can only use this method when SSL has not been enabled through the client configuration, you must also establish a connection to a server first. When calling this method, it will block until the SSL handshake has finished.

Example

<?php

$client = new Swoole\Client(SWOOLE_SOCK_TCP);

if(!$client->connect('127.0.0.1', 9501, -1))
{
    exit("connect failed. Error: {$client->errCode}\n");
}

$client->send("Hello World! - No SSL\n");

echo "Data from server: " . $client->recv() . "\n\n";

// Enable SSL
if($client->enableSSL())
{
    // The handshake is complete, the data sent and received at this time is encrypted using SSL
    $client->send("Hello World! - With SSL\n");
    echo $client->recv();
}

$client->close();