Skip to content

Instantly share code, notes, and snippets.

@m0veax
Forked from htp/curl-websocket.sh
Last active July 25, 2024 05:23
Show Gist options
  • Save m0veax/3ef9d5b676d6bdf28243168794b35708 to your computer and use it in GitHub Desktop.
Save m0veax/3ef9d5b676d6bdf28243168794b35708 to your computer and use it in GitHub Desktop.
Test a WebSocket using php fsockopen, bit chaotic, but working for me. You can use that for a HTTP Sensor in PRTG or Nagios
<?php
error_reporting(-1);
$host = "your-host.tld"; // your websocket url without protocol part
$SecWebsocketKey = "your key from websocket connection"; // try wireshark or developer Tools to get this
$origin = "http://$host/"; // origin for the header
$host = 'localhost'; //where is the websocket server
$port = 8080; // Your Websocket Port
$head = "GET / HTTP/1.1"."\r\n".
"Upgrade: WebSocket"."\r\n".
"Connection: Upgrade"."\r\n".
"Origin: $$origin"."\r\n".
"Host: $host"."\r\n".
"Content-Length: 0\r\n".
"Sec-WebSocket-Key: $SecWebsocketKey"."\r\n".
"Sec-WebSocket-Protocol: wamp"."\r\n".
"Accept-Encoding: gzip, deflate"."\r\n".
"Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7"."\r\n".
"Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits"."\r\n".
"Sec-WebSocket-Version: 13"."\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);
fclose($sock);
// Check for Protocol Upgrade Header in the Response
if(strpos($headers, 'HTTP/1.1 101 Switching Protocols') !== false) {
echo "OK";
} else {
echo "Websocket Connection Error";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment