使用PHP客户端连接到websocket

时间:2014-03-13 06:40:49

标签: php sockets

我试图将基于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 websocket package "\x00DATA\xff"
$retdata = trim($wsdata,"\x00\xff"); //extracts data
////WebSocket handshake
fclose($sock);

echo $retdata;

5 个答案:

答案 0 :(得分:9)

我可能更喜欢使用现有的websocket客户端库(可能是https://github.com/gabrielbull/php-websocket-clienthttps://github.com/Devristo/phpws/tree/master/src/Devristo/Phpws/Client?)而不是自己滚动,但我至少通过使用它来连接:

$head = "GET / HTTP/1.1"."\r\n".
    "Host: $host"."\r\n".
    "Upgrade: websocket"."\r\n".
    "Connection: Upgrade"."\r\n".
    "Sec-WebSocket-Key: asdasdaas76da7sd6asd6as7d"."\r\n".
    "Sec-WebSocket-Version: 13"."\r\n".
    "Content-Length: ".strlen($data)."\r\n"."\r\n";

我的服务器正在使用TLS / SSL,所以我还需要:

$sock = fsockopen('tls://'.$host, $port, $errno, $errstr, 2);

完整的协议规范是:https://tools.ietf.org/rfc/rfc6455.txt

答案 1 :(得分:6)

您的标头必须包含:

Sec-WebSocket-Key: (some value) 
Sec-WebSocket-Version: 13

成功连接。
建立连接后,还需要使用hybi10帧编码 见:https://tools.ietf.org/rfc/rfc6455.txt - 虽然有点干。

我做了这个工作的例子:

<?php
$sp=websocket_open('127.0.0.1/ws_request.php?param=php_test',$errstr);
websocket_write($sp,"Websocket request message");
echo websocket_read($sp,true);

$sp=websocket_open('127.0.0.1:8080/ws_request.php?param=php_test',$errstr);
websocket_write($sp,"Websocket request message");
echo websocket_read($sp,true);

function websocket_open($url){
  $key=base64_encode(uniqid());
  $query=parse_url($url);
  $header="GET / HTTP/1.1\r\n"
    ."pragma: no-cache\r\n"
    ."cache-control: no-cache\r\n"
    ."Upgrade: WebSocket\r\n"
    ."Connection: Upgrade\r\n"
    ."Sec-WebSocket-Key: $key\r\n"
    ."Sec-WebSocket-Version: 13\r\n"
    ."\r\n";
  $sp=fsockopen($query['host'],$query['port'], $errno, $errstr,1); 
  if(!$sp) die("Unable to connect to server ".$url);
  // Ask for connection upgrade to websocket
  fwrite($sp,$header);
  stream_set_timeout($sp,5);
  $reaponse_header=fread($sp, 1024);
  if(!strpos($reaponse_header," 101 ") 
      || !strpos($reaponse_header,'Sec-WebSocket-Accept: ')){
    die("Server did not accept to upgrade connection to websocket"
        .$reaponse_header);
  }    
  return $sp;
}

function websocket_write($sp, $data,$final=true){
  // Assamble header: FINal 0x80 | Opcode 0x02
  $header=chr(($final?0x80:0) | 0x02); // 0x02 binary

  // Mask 0x80 | payload length (0-125) 
  if(strlen($data)<126) $header.=chr(0x80 | strlen($data));  
  elseif (strlen($data)<0xFFFF) $header.=chr(0x80 | 126) . pack("n",strlen($data));
  elseif(PHP_INT_SIZE>4) // 64 bit 
    $header.=chr(0x80 | 127) . pack("Q",strlen($data));
  else  // 32 bit (pack Q dosen't work)
    $header.=chr(0x80 | 127) . pack("N",0) . pack("N",strlen($data));

  // Add mask
  $mask=pack("N",rand(1,0x7FFFFFFF));       
  $header.=$mask;

  // Mask application data. 
  for($i = 0; $i < strlen($data); $i++)
    $data[$i]=chr(ord($data[$i]) ^ ord($mask[$i % 4]));

  return fwrite($sp,$header.$data);    
}

function websocket_read($sp,$wait_for_end=true,&$err=''){
  $out_buffer="";
  do{
    // Read header
    $header=fread($sp,2);
    if(!$header) die("Reading header from websocket failed");
    $opcode = ord($header[0]) & 0x0F;
    $final = ord($header[0]) & 0x80;
    $masked = ord($header[1]) & 0x80;
    $payload_len = ord($header[1]) & 0x7F;

    // Get payload length extensions
    $ext_len = 0;
    if($payload_len >= 0x7E){
      $ext_len = 2;
      if($payload_len == 0x7F) $ext_len = 8;
      $ext=fread($sp,$ext_len);
      if(!$ext) die("Reading header extension from websocket failed");

      // Set extented paylod length
      $payload_len= 0;
      for($i=0;$i<$ext_len;$i++) 
        $payload_len += ord($header[$i]) << ($ext_len-$i-1)*8;
    }

    // Get Mask key
    if($masked){
      $mask=fread($sp,4);
      if(!$mask) die("Reading header mask from websocket failed");
    }

    // Get payload
    $frame_data='';
    do{
      $frame= fread($sp,$payload_len);
      if(!$frame) die("Reading from websocket failed.");
      $payload_len -= strlen($frame);
      $frame_data.=$frame;
    }while($payload_len>0);    

    // if opcode ping, reuse headers to send a pong and continue to read
    if($opcode==9){
      // Assamble header: FINal 0x80 | Opcode 0x02
      $header[0]=chr(($final?0x80:0) | 0x0A); // 0x0A Pong
      fwrite($sp,$header.$ext.$mask.$frame_data);

    // Recieve and unmask data
    }elseif($opcode<3){
      $data="";
      if($masked)
        for ($i = 0; $i < $data_len; $i++) 
          $data.= $frame_data[$i] ^ $mask[$i % 4];
      else    
        $data.= $frame_data;
      $out_buffer.=$data;
    }

    // wait for Final 
  }while($wait_for_end && !$final);

  return $out_buffer;
}

您可以在此处获取完整版:https://github.com/paragi/PHP-websocket-client

答案 2 :(得分:0)

使用纯PHP连接到WSS stream

公共binance wss api的示例。

<?php
$sock = stream_socket_client("ssl://stream.binance.com:9443",$error,$errnum,30,STREAM_CLIENT_CONNECT,stream_context_create(null));
if (!$sock) {
    echo "[$errnum] $error" . PHP_EOL;
} else {
  echo "Connected - Do NOT get rekt!" . PHP_EOL;
  fwrite($sock, "GET /stream?streams=btcusdt@kline_1m HTTP/1.1\r\nHost: stream.binance.com:9443\r\nAccept: */*\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: ".rand(0,999)."\r\n\r\n");
  while (!feof($sock)) {
    var_dump(explode(",",fgets($sock, 512)));
  }
}

答案 3 :(得分:0)

400错误是因为标题中缺少 Host Origin

$key=base64_encode(openssl_random_pseudo_bytes(16));
$query=parse_url($url);
$local = "http://".$query['host'];
if (isset($_SERVER['REMOTE_ADDR'])) $local = "http://".$_SERVER['REMOTE_ADDR'];  
$header="GET / HTTP/1.1\r\n"
    ."Host: ".$query['host']."\r\n"
    ."Origin: ".$local."\r\n"
    ."Pragma: no-cache\r\n"
    ."Cache-Control: no-cache\r\n"
    ."Upgrade: websocket\r\n"
    ."Connection: Upgrade\r\n"
    ."Sec-WebSocket-Key: $key\r\n"
    ."Sec-WebSocket-Version: 13\r\n"
    ."\r\n";

答案 4 :(得分:0)

https://github.com/ratchetphp/Pawl

我花了大约6个小时尝试了不同的解决方案,然后尝试了来自各种stackoverflow线程的10种不同的解决方案,但是没有任何效果。 场景: 棘轮服务器 我能够通过reactjs连接 我无法通过php连接, 预期结果:从php-client向所有连接的react client发送消息。我提供的git repo是我一直在寻找的幸运假期。