PHP code example of yidas / socket

1. Go to this page and download the library: Download yidas/socket 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/ */

    

yidas / socket example snippets


try {

    $socket = new \yidas\socket\Client([
        'protocol' => 'tcp',
        'host' => 'smtp.your.com',
        'port' => '25',
        // 'domain' => AF_INET,
    ]);

} catch (Exception $e) {
    
    die("Failed to connect: {$e->getMessage()} (Code: {$e->getCode()})");
}

echo $socket->read();
// ...
$socket->write('STARTTLS');
echo $socket->read();
$result = $socket->enableCrypto();
// ...

$socket->close();

$socket = new \yidas\socket\Client();

try {

  $socket->stream_socket_client('tcp://smtp.your.com:25', $errorCode, $errorMsg, 15);

} catch (Exception $e) {
    
    die("Failed to connect: {$e->getMessage()} (Code: {$e->getCode()})");
}

$socket->fread(1024);
// ...
$socket->fwrite('STARTTLS');
echo $socket->fread(1024);
$result = $socket->stream_socket_enable_crypto(true, STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT);
// ...

$socket->fclose();