I am trying to send data to PHP websocket server, although it sends the data but the data received is a garbage values. How can fix this to get correct values posted to websocket php server?
Below is my websocket php client script
<?php
$host = 'example.com:9000/server.php'; //where is the websocket server
$port = 9000; //ssl
$local = "http://localhost/"; //url where this script run
$data = json_encode(array("server_msg"=> "1","device_id"=> "DDD-123455678")); //data to be send
$head = "GET / HTTP/1.1"."
".
"Host: $host"."
".
"Upgrade: websocket"."
".
"Connection: Upgrade"."
".
"Sec-WebSocket-Key: asdasdaas76da7sd6asd6as7d"."
".
"Sec-WebSocket-Version: 13"."
".
"Content-Length: ".strlen($data)."
"."
";
////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;
?>
Thanks
Hi,
I have already tried it and it gives me error as below:
Fatal error: Uncaught exception 'WebSocket\ConnectionException' with message 'Connection to 'ws://************/server.php' in /var/www/webclientphp/vendor/textalk/websocket/lib/Client.php on line 149
WebSocket\ConnectionException: Connection to 'ws://************/server.php' failed: Server sent invalid upgrade response: HTTP/1.1 101 Web Socket Protocol Handshake Upgrade: websocket Connection: Upgrade WebSocket-Origin: ************ WebSocket-Location: ws://************:9000/demo/shout.php Sec-WebSocket-Accept:Kfh9QIsMVZcl6xEPYxPHzW8SZ8w= in /var/www/webclientphp/vendor/textalk/websocket/lib/Client.php on line 149
Please help