I\'m trying to send a fire-and-forget request from PHP to my websocket aws api gateway.
I\'ve set up an action called \"sendmessage\".
This is the code I\'m
You probably need to use http_build_query();
function like :
$content = http_build_query($content);
and use form post to send message, so try the following code to check if socket connection is success, probably mistake in your code is pfsockopen()
should be @fsockopen()
Edit this for your requirements :
$protocol = "ssl";
$host = ".amazonaws.com";
$port = 443;
$path = "//";
$timeout = 2000;
$socket = @fsockopen($protocol . "://" . $host, $port,
$errno, $errstr, $timeout);
if($socket === false) { return false; };
$content = "{'action': 'sendmessage', 'data': 'test'}";
$body = "POST $path HTTP/1.1\r\n";
$body .= "Host: $host\r\n";
$body .= "Referer: yourClass (v.".version() .")\r\n";
$body .= "Content-type: application/json\r\n";
$body .= "Content-Length: ".strlen($content)."\r\n";
$body .= "Connection: Close\r\n\r\n";
$body .= "$content";
fwrite($socket, $body);
fclose($socket);
This code works fine in my site as a function with
$out .= "Content-type: application/x-www-form-urlencoded\r\n"; instead of json
function flexy_Request($url, $_data) {
// parse the given URL
$url = parse_url($url);
if ($url === false || !isset($url['host']) || !isset($url['path'])) {
return false;
}
// extract host and path:
$host = $url['host'];
$path = $url['path'];
// open a socket connection on port 80
// use localhost in case of issues with NATs (hairpinning)
$fp = @fsockopen($host, 80);
if($fp===false) { return false; };
$data = http_build_query($_data);
$out = "POST $path HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Referer: Myclass (v.". flexy_version() .")\r\n";
$out .= "Content-type: application/json\r\n";
$out .= "Content-Length: ".strlen($data)."\r\n";
$out .= "Connection: Close\r\n\r\n";
$out .= "$data";
$number_bytes_sent = fwrite($fp, $out);
fclose($fp);
return $number_bytes_sent; // or false on fwrite() error
}