小编Joh*_*lan的帖子

使用PHP客户端连接到websocket

我正在尝试将基于PHP的客户端连接到websocket服务器.

这是我一直在使用的代码,已经在不同的论坛上广泛发布.但由于某种原因,我无法让它发挥作用.

任何帮助,将不胜感激.

$host = 'host';  //where is the websocket server
$port = 443; //ssl
$local = "http://www.example.com/";  //url where this script run
$data = '{"id": 2,"command": "server_info"}';  //data to be send

$head =        "GET / HTTP/1.1"."\r\n".
               "Upgrade: WebSocket"."\r\n".
               "Connection: Upgrade"."\r\n".
               "Origin: $local"."\r\n".
               "Host: $host"."\r\n".
               "Content-Length: ".strlen($data)."\r\n"."\r\n";
////WebSocket handshake
$sock = fsockopen($host, $port, $errno, $errstr, 2);
fwrite($sock, $head ) or die('error:'.$errno.':'.$errstr);
$headers = fread($sock, 2000);
fwrite($sock, "\x00$data\xff" ) or die('error:'.$errno.':'.$errstr);
$wsdata = fread($sock, 2000);  //receives the data included in the …
Run Code Online (Sandbox Code Playgroud)

php sockets

11
推荐指数
3
解决办法
3万
查看次数

标签 统计

php ×1

sockets ×1