/** * Sends a JSON-RPC request * @param Request $request A Tivoka request * @return Request if sent as a batch request the BatchRequest object will be returned */ public function send(Request $request) { if (func_num_args() > 1) { $request = func_get_args(); } if (is_array($request)) { $request = new BatchRequest($request); } if (!$request instanceof Request) { throw new Exception\Exception('Invalid data type to be sent to server'); } // preparing connection... $context = array('http' => array('content' => $request->getRequest($this->spec), 'header' => "Content-Type: application/json\r\n" . "Connection: Close\r\n", 'method' => 'POST', 'timeout' => $this->timeout)); foreach ($this->headers as $label => $value) { $context['http']['header'] .= $label . ": " . $value . "\r\n"; } //sending... $response = @file_get_contents($this->target, false, stream_context_create($context)); if ($response === FALSE) { throw new Exception\ConnectionException('Connection to "' . $this->target . '" failed'); } $request->setResponse($response); $request->setHeaders($http_response_header); return $request; }
/** * Sends a JSON-RPC request * @param Request $request A Tivoka request * @return Request if sent as a batch request the BatchRequest object will be returned */ public function send(Request $request) { if (func_num_args() > 1) { $request = func_get_args(); } if (is_array($request)) { $request = new BatchRequest($request); } if (!$request instanceof Request) { throw new Exception\Exception('Invalid data type to be sent to server'); } if (extension_loaded('curl')) { $headers = array('Content-Type: application/json', 'Connection: Close'); foreach ($this->headers as $label => $value) { $headers[] = $label . ": " . $value; } $response_headers = array(); $headerFunction = function ($ch, $header) use(&$response_headers) { $header2 = rtrim($header, "\r\n"); if ($header2 != '') { $response_headers[] = $header2; } return strlen($header); // Use original header length! }; $ch = curl_init($this->target); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $request->getRequest($this->spec)); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_HEADERFUNCTION, $headerFunction); $response = @curl_exec($ch); curl_close($ch); } elseif (ini_get('allow_url_fopen')) { // preparing connection... $context = array('http' => array('content' => $request->getRequest($this->spec), 'header' => "Content-Type: application/json\r\n" . "Connection: Close\r\n", 'method' => 'POST', 'timeout' => $this->timeout)); foreach ($this->headers as $label => $value) { $context['http']['header'] .= $label . ": " . $value . "\r\n"; } //sending... $response = @file_get_contents($this->target, false, stream_context_create($context)); $response_headers = $http_response_header; } else { throw new Exception\ConnectionException('Install cURL extension or enable allow_url_fopen'); } if ($response === FALSE) { throw new Exception\ConnectionException('Connection to "' . $this->target . '" failed'); } $request->setResponse($response); $request->setHeaders($response_headers); return $request; }
/** * Sends a JSON-RPC request over plain WebSocket. * @param Request $request,... A Tivoka request. * @return Request|BatchRequest If sent as a batch request the BatchRequest object will be returned. */ public function send(Request $request) { if (func_num_args() > 1) { $request = func_get_args(); } if (is_array($request)) { $request = new BatchRequest($request); } if (!$request instanceof Request) { throw new Exception\Exception('Invalid data type to be sent to server'); } // sending request $this->ws_client->send($request->getRequest($this->spec), 'text', true); // read server respons $response = $this->ws_client->receive(); if (($opcode = $this->ws_client->getLastOpcode()) !== 'text') { throw new Exception\ConnectionException("Received non-text frame of type '{$opcode}' with text: " . $response); } $request->setResponse($response); return $request; }
/** * Sends a JSON-RPC request over plain TCP. * @param Request $request,... A Tivoka request. * @return Request|BatchRequest If sent as a batch request the BatchRequest object will be returned. */ public function send(Request $request) { // connect on first call if (!isset($this->socket)) { $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout); // check for success if ($this->socket === false) { throw new Exception\ConnectionException('Connection to "' . $this->host . ':' . $this->port . '" failed (errno ' . $errno . '): ' . $errstr); } stream_set_timeout($this->socket, $this->timeout); } if (func_num_args() > 1) { $request = func_get_args(); } if (is_array($request)) { $request = new BatchRequest($request); } if (!$request instanceof Request) { throw new Exception\Exception('Invalid data type to be sent to server'); } // sending request fwrite($this->socket, $request->getRequest($this->spec)); fwrite($this->socket, "\n"); fflush($this->socket); // read server respons $response = fgets($this->socket); if ($response === false) { throw new Exception\ConnectionException('Connection to "' . $this->host . ':' . $this->port . '" failed'); } $request->setResponse($response); return $request; }