/** * Converts a request to a log message * * @param Elastica_Request $request * @return string Request log message */ protected function _convertRequest(Elastica_Request $request) { $message = 'curl -X' . strtoupper($request->getMethod()) . ' '; $message .= 'http://' . $request->getClient()->getHost() . ':' . $request->getClient()->getPort() . '/'; $message .= $request->getPath(); $message .= ' -d \'' . json_encode($request->getData()) . '\''; return $message; }
/** * Converts a request to a log message * * @param Elastica_Request $request * @return string Request log message */ protected function _convertRequest(Elastica_Request $request) { $message = 'curl -X' . strtoupper($request->getMethod()) . ' '; $message .= '\'http://' . $request->getClient()->getHost() . ':' . $request->getClient()->getPort() . '/'; $message .= $request->getPath(); $query = $request->getQuery(); if (!empty($query)) { $message .= '?' . http_build_query($query); } $message .= '\''; $data = $request->getData(); if (!empty($data)) { $message .= ' -d \'' . json_encode($data) . '\''; } return $message; }
/** * Makes calls to the elasticsearch server * * All calls that are made to the server are down over this function * * @param Elastica_Request $request Request object * @return Elastica_Response Response object */ protected function _callService(Elastica_Request $request) { $conn = curl_init(); $baseUri = 'http://' . $this->getHost() . ':' . $this->getPort() . '/'; $baseUri .= $request->getPath(); curl_setopt($conn, CURLOPT_URL, $baseUri); curl_setopt($conn, CURLOPT_TIMEOUT, self::TIMEOUT); curl_setopt($conn, CURLOPT_PORT, $this->getPort()); curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1); curl_setopt($conn, CURLOPT_CUSTOMREQUEST, $request->getMethod()); if (!empty($this->_headers)) { $headers = array(); while (list($header, $headerValue) = each($this->_headers)) { array_push($headers, $header . ': ' . $headerValue); } curl_setopt($conn, CURLOPT_HTTPHEADER, $headers); } // TODO: REFACTOR $data = $request->getData(); if (!empty($data)) { if (is_array($data)) { $content = json_encode($data); } else { $content = $data; } // Escaping of / not necessary. Causes problems in base64 encoding of files $content = str_replace('\\/', '/', $content); curl_setopt($conn, CURLOPT_POSTFIELDS, $content); } $start = microtime(true); $response = curl_exec($conn); $end = microtime(true); // Checks if error exists $errorNumber = curl_errno($conn); $response = new Elastica_Response($response); if (defined('DEBUG') && DEBUG) { $response->setQueryTime($end - $start); $response->setTransferInfo(curl_getinfo($conn)); } if ($response->hasError()) { throw new Elastica_Exception_Response($response); } if ($errorNumber > 0) { throw new Elastica_Exception_Client($errorNumber, $request, $response); } return $response; }