1. Go to this page and download the library: Download spatie/simple-tcp-client 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/ */
use Spatie\SimpleTcpClient\TcpClient;
$client = new TcpClient('example.com', 80);
$client->connect();
$client->send("Hello, server!");
$response = $client->receive(); // Read response from server
echo $response;
$response = $client->receive(1024);
use Spatie\SimpleTcpClient\TcpClient;
use Spatie\SimpleTcpClient\Exceptions\ConnectionTimeout;
$client = new TcpClient('slow-server.com', 80, 5_000); // 5000ms (5 second) timeout
try {
$client->connect();
} catch (ConnectionTimeout $exception) {
echo "Server took too long to respond: " . $exception->getMessage();
}