originphp / socket
OriginPHP Socket
Installs: 4 847
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/originphp/socket
Requires
- php: >=7.3.0
Requires (Dev)
- phpstan/phpstan: ^0.12.64
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2025-10-04 21:04:38 UTC
README
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 ] ] ]);