Skip to content

Instantly share code, notes, and snippets.

@andrioid
Created April 18, 2015 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrioid/dea264b5075b8ff6a8e8 to your computer and use it in GitHub Desktop.
Save andrioid/dea264b5075b8ff6a8e8 to your computer and use it in GitHub Desktop.
bleh
// HTTP Fire and Forget (Asynchronous, mostly - hey it's PHP!)
public static function SendHTTP($action, $data = null, $service = 'admin', $host = false, $port = false, $key = false){
if ($port == false) { $port = Config::Get('MBISS_PORT'); }
if ($host == false) { $host = Config::Get('MBISS_HOST'); }
if ($key == false) { $key = Config::Get('MBISS_KEY'); }
if (data == null) { $data = array(); }
if ($key == false) { $key = ''; }
if (empty($host) || empty($port)) { return false; }
$sock = stream_socket_client(sprintf("tcp://%s:%d", $host, $port), $errno, $errstr, 0.5, STREAM_CLIENT_ASYNC_CONNECT);
if (!$sock) {
Syslog::Log(Syslog::WARNING, 'ScreenWS::SendHTTP', 'Unable to open socket', $host, $action, print_r($data, true));
return false;
}
$read = null;
$except = null;
$write = array($sock);
$sel = stream_select($read, $write, $except, 1);
if ($sel === false) { return false; } // Ignore if stream returned an error
if ($sel > 0) {
$pdata = sprintf("action=%s&key=%s&data=%s",
urlencode($action),
urlencode($key),
urlencode(json_encode($data))
);
$len = strlen($pdata);
try {
fwrite($sock, sprintf("POST /%s HTTP/1.0\r\n", $service));
//fwrite($sock, "Host: secure.example.com\r\n");
fwrite($sock, "Content-type: application/x-www-form-urlencoded\r\n");
fwrite($sock, sprintf("Content-length: %d\r\n", $len));
fwrite($sock, "Accept: */*\r\n");
fwrite($sock, "\r\n");
fwrite($sock, "$pdata\r\n");
fwrite($sock, "\r\n");
fclose($sock);
} catch (Exception $e) {
Syslog::Log(Syslog::WARNING, 'ScreenWS::SendHTTP', 'Failed to write to socket', $host, $action, print_r($data, true));
}
return true;
}
return false;
}//function Send HTTP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment