PHP Socket (TCP, TLS, UDP, SSL) Server/Client Library
- PHP 7.4 or higher
- PHP Sockets Extension
composer require initphp/socket
Supported Types :
- TCP
- UDP
- TLS
- SSL
\InitPHP\Socket\Socket::class It allows you to easily create socket server or client.
public static function server(int $handler = Socket::TCP, string $host = '', int $port = 0, null|string|float $argument = null): \InitPHP\Socket\Interfaces\SocketServerInterface- $handler:- Socket::SSL,- Socket::TCP,- Socket::TLSor- Socket::UDP
- $host: Identifies the socket host. If not defined or left blank, it will throw an error.
- $port: Identifies the socket port. If not defined or left blank, it will throw an error.
- $argument: This value is the value that will be sent as 3 parameters to the constructor method of the handler.- SSL or TLS = (float) Defines the timeout period.
- UDP or TCP = (string) Defines the protocol family to be used by the socket. "v4", "v6" or "unix"
 
public static function client(int $handler = self::TCP, string $host = '', int $port = 0, null|string|float $argument = null): \InitPHP\Socket\Interfaces\SocketClientInterface- $handler:- Socket::SSL,- Socket::TCP,- Socket::TLSor- Socket::UDP
- $host: Identifies the socket host. If not defined or left blank, it will throw an error.
- $port: Identifies the socket port. If not defined or left blank, it will throw an error.
- $argument: This value is the value that will be sent as 3 parameters to the constructor method of the handler.- SSL or TLS = (float) Defines the timeout period.
- UDP or TCP = (string) Defines the protocol family to be used by the socket. "v4", "v6" or "unix"
 
connection() : Initiates the socket connection.
public function connection(): self;disconnect() : Terminates the connection.
public function disconnect(): bool;read() : Reads data from socket.
public function read(int $length = 1024): ?string;write() : Writes data to the socket
public function write(string $string): ?int;live() :
public function live(callable $callback): void;wait() :
public function wait(int $second): void;broadcast() :
public function broadcast(string $message, array|string|int|null $clients = null): bool;TLS and SSL work similarly.
There are some additional methods you can use from TLS and SSL sockets.
timeout() : Defines the timeout period of the current.
public function timeout(int $second): self;blocking() : Sets the blocking mode of the current.
public function blocking(bool $mode = true): self;crypto() : Turns encryption on or off on a connected socket.
public function crypto(?string $method = null): self;Possible values for $method are;
- "sslv2"
- "sslv3"
- "sslv23"
- "any"
- "tls"
- "tlsv1.0"
- "tlsv1.1"
- "tlsv1.2"
- NULL
option() : Defines connection options for SSL and TLS. see; https://www.php.net/manual/en/context.ssl.php
public function option(string $key, mixed $value): self;Example :
require_once "../vendor/autoload.php";
use \InitPHP\Socket\Socket;
use \InitPHP\Socket\Interfaces\{SocketServerInterface, SocketServerClientInterface};
$server = Socket::server(Socket::TLS, '127.0.0.1', 8080);
$server->connection();
$server->live(function (SocketServerInterface $socket, SocketServerClientInterface $client) {
    $read = $client->read();
    if (!$read) {
        return;
    }
    if (in_array($read, ['exit', 'quit'])) {
            $client->push("Goodbye!");
            $client->close();
        return;
    } else if (preg_match('/^REGISTER\s+([\w]{3,})$/i', $read, $matches)) {
        // REGISTER admin
        $name = trim(mb_substr($read, 9));
        $socket->clientRegister($name, $client);
    } else if (preg_match('/^SEND\s@([\w]+)\s(.*)$/i', $read, $matches)) {
        // SEND @admin Hello World
        $pushSocketName = $matches[1];
        $message = $matches[2];
        $socket->broadcast($message, [$pushSocketName])
    } else {
        $message = trim($read);
        !empty($message) && $socket->broadcast($message);
    }
});Example :
require_once "../vendor/autoload.php";
use \InitPHP\Socket\Socket;
$client = Socket::client(Socket::SSL, 'smtp.gmail.com', 465);
$client->option('verify_peer', false)
    ->option('verify_peer_name', false);
$client->connection();
$client->write('EHLO [127.0.0.1]');
echo $client->read();In the above example, a simple smtp connection to gmail is made.
Copyright © 2022 MIT License