开发者

How to open TCP connection in PHP

开发者 https://www.devze.com 2023-02-16 17:54 出处:网络
How to open TCP connection in PHP and send some String over that connection ( for exam开发者_如何学Pythonple \"test\") ?You can create a socket with socket_create, open it with socket_connect and writ

How to open TCP connection in PHP and send some String over that connection ( for exam开发者_如何学Pythonple "test") ?


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


You can try the example from 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);
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消