How to open a TCP connection in PHP

How to open a TCP connection in PHP and send some string on this connection (for example, "test")?

+5
source share
2 answers

You can try the example at this link :

<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET / HTTP/1.1\r\n";
    $out .= "Host: www.example.com\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>
+2
source

You can create a socket with socket_create, open it with socket_connect and write using socket_write. socket_write documentation on php.net

+9
source

All Articles