yidas/socket

0.1 2024-01-11 10:01 UTC

This package is auto-updated.

Last update: 2024-04-11 12:01:27 UTC


README

Modern PHP Socket class based on native infra (pure PHP, CI, Yii, Laravel support)

Latest Stable Version License

DEMONSTRATION

Client

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();

Native function support:

$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();