PHP code example of rtm-ctrlz / keepalive

1. Go to this page and download the library: Download rtm-ctrlz/keepalive library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rtm-ctrlz / keepalive example snippets


socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 1);

// create socket
$socket = \socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// enable tcp-keepalive
\RtmCtrlz\Keepalive\Keepalive::enable($socket, 45, 5, 3);

// create stream
$stream = \stream_socket_client('tcp://127.0.0.1:80);
// get socket
$socket = \socket_import_stream($stream);
// enable tcp-keepalive
\RtmCtrlz\Keepalive\Keepalive::enable($socket, 45, 5, 3);

// create stream
// NOTE: stream has "tcp" proto
$stream = \stream_socket_client(
    'tcp://127.0.0.1:443',         // target
    $errno,                        // error number
    $errstr,                       // error description
    1.1,                           // timeout
    STREAM_CLIENT_CONNECT,         // flags
    stream_context_create(         // context
        [
            'ssl' => [
                // ... ssl options
            ],
        ]
    )
);

// get socket
$socket = \socket_import_stream($stream);
// enable tcp-keepalive
\RtmCtrlz\Keepalive\Keepalive::enable($socket, 45, 5, 3);
// enable encryption
\stream_socket_enable_crypto($stream, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);