title: Connecting by php to a python websocket server
body: <p>I have a working websocket Server (python + Tornado) which accepts Connections on port 8973. I can connect by a easy JavaScript / jquery instruction like:</p>
  
  <pre><code>ws = new WebSocket("ws://192.168.41.170:8973/rt");
  </code></pre>
  
  <p>But I need my php script to connect to this websocket server and send a message. I tried all most available solutions like</p>
  
  <p><a href="https://github.com/lemmingzshadow/php-websocket/" rel="nofollow">https://github.com/lemmingzshadow/php-websocket/</a></p>
  
  <pre><code>$host = '192.168.41.170';  //where is the websocket server
  $port = 8973;
  $local = "http://192.168.41.2/";  //url where this script run
  $data = 'hello world!';  //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"
  fclose($sock);
  </code></pre>
  
  <p>But this all dont work. Has anybody a working code snippet? I dont need a php-websocket server! Thanks </p>