如何在 PHP 中打开 TCP 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5309400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:57:28  来源:igfitidea点击:

How to open TCP connection in PHP

php

提问by Damir

How to open TCP connection in PHP and send some String over that connection ( for example "test") ?

如何在 PHP 中打开 TCP 连接并通过该连接发送一些字符串(例如“测试”)?

采纳答案by Abdullah Md. Zubair

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);
}
?>

回答by alfmartinez

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

您可以使用 socket_create 创建一个套接字,使用 socket_connect 打开它并使用 socket_write 写入。php.net 上的 socket_write 文档