diff --git a/src/LaunchDarkly/EventProcessor.php b/src/LaunchDarkly/EventProcessor.php index 30d8016dc..b2c813673 100644 --- a/src/LaunchDarkly/EventProcessor.php +++ b/src/LaunchDarkly/EventProcessor.php @@ -63,86 +63,28 @@ protected function flush() { return; } - $socket = $this->createSocket(); - - if (!$socket) { - error_log("LaunchDarkly unable to open socket"); - return; - } $payload = json_encode($this->_queue); - $body = $this->createBody($payload); + $args = $this->createArgs($payload); - return $this->makeRequest($socket, $body); + return $this->makeRequest($args); } - private function createSocket() { - if ($this->_socket_failed) { - return false; - } - - $protocol = $this->_ssl ? "ssl" : "tcp"; - - try { - - $socket = @pfsockopen($protocol . "://" . $this->_host, $this->_port, $errno, $errstr, $this->_timeout); - - if ($errno != 0) { - $this->_socket_failed = true; - return false; - } - - return $socket; - } catch (Exception $e) { - error_log("LaunchDarkly caught $e"); - $this->socket_failed = true; - return false; - } - } - - private function createBody($content) { - $req = ""; - $req.= "POST /api/events/bulk HTTP/1.1\r\n"; - $req.= "Host: " . $this->_host . "\r\n"; - $req.= "Content-Type: application/json\r\n"; - $req.= "Authorization: api_key " . $this->_apiKey . "\r\n"; - $req.= "User-Agent: PHPClient/" . LDClient::VERSION . "\r\n"; - $req.= "Accept: application/json\r\n"; - $req.= "Content-length: " . strlen($content) . "\r\n"; - $req.= "\r\n"; - $req.= $content; - return $req; + private function createArgs($payload) { + $scheme = $this->_ssl ? "https://" : "http://"; + $args = " -X POST"; + $args.= " -H 'Content-Type: application/json'"; + $args.= " -H " . escapeshellarg("Authorization: api_key " . $this->_apiKey); + $args.= " -H 'User-Agent: PHPClient/" . LDClient::VERSION . "'"; + $args.= " -H 'Accept: application/json'"; + $args.= " -d " . escapeshellarg($payload); + $args.= " " . escapeshellarg($scheme . $this->_host . ":" . $this->_port . "/api/events/bulk"); + return $args; } - private function makeRequest($socket, $req, $retry = true) { - $bytes_written = 0; - $bytes_total = strlen($req); - $closed = false; - - while (!$closed && $bytes_written < $bytes_total) { - try { - $written = @fwrite($socket, substr($req, $bytes_written)); - } catch (Exception $e) { - error_log("LaunchDarkly caught $e"); - $closed = true; - } - if (!isset($written) || !$written) { - $closed = true; - } else { - $bytes_written += $written; - } - } - - if ($closed) { - fclose($socket); - if ($retry) { - error_log("LaunchDarkly retrying send"); - $socket = $this->createSocket(); - if ($socket) return $this->makeRequest($socket, $req, false); - } - return false; - } - + private function makeRequest($args) { + $cmd = "/usr/bin/env curl " . $args . ">> /dev/null 2>&1 &"; + shell_exec($cmd); return true; }