Skip to content

originphp/socket

Repository files navigation

Socket

license build coverage

The Socket class is a lightweight wrapper which makes working with sockets easy.

Installation

To install this package

$ composer require originphp/socket

Using Sockets

use Origin\Socket\Socket;
$socket = new Socket([
    'host' => 'localhost',
    'protocol' => 'tcp',
    'port' => 25,
    'timeout' => 30,
    'persistent' => false,
]);

if ($socket->connect()) {
    $socket->write("HELO mydomain.com\r\n");
    $result = $socket->read();
}

$socket->disconnect();

You can can also enable encryption using ssl or tls, you can also specify versions e.g. sslv2, sslv23, sslv3, tlsv1, tlsv11 and tlsv12. See the PHP manual for more information on the encryption methods.

To enable encryption

$socket->enableEncryption('tls');
$socket->enableEncryption('ssl');

To disable encryption

$socket->disableEncryption('tls');
$socket->disableEncryption('ssl');

Host/IP Address

To get the IP address of the connection

$ipAddress = $socket->address();

To get the hostname

$hostname = $socket->host();

Stream Contexts

When creating a Socket you can also provide context options that will be used to create a stream context.

$socket = new Socket([
    'host' => 'example.com',
    'protocol' => 'tcp',
    'port' => 443,
    'timeout' => 30,
    'persistent' => false,
    'context' => [
        'ssl' => [
            'verify_peer' => false
        ]
    ]
]);