/** * 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 based on this index * * It's possible to make any REST query directly over this method * * @param string $path Path to call * @param string $method Rest method to use (GET, POST, DELETE, PUT) * @param array $data OPTIONAL Arguments as array * @param array $query OPTIONAL Query params * @return Elastica_Response Response object */ public function request($path, $method, $data = array(), array $query = array()) { $request = new Elastica_Request($this, $path, $method, $data, $query); return $request->send(); }
/** * 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; }
/** * Sends request to server * * @return Elastica_Response Response object */ public function send() { $log = new Elastica_Log($this->getClient()); $log->log($this); $transport = $this->getTransport(); $servers = $this->getClient()->getConfig('servers'); /* // Integration of temp file $dir = sys_get_temp_dir(); $name = 'elasticaServers.json'; $file = $dir . DIRECTORY_SEPARATOR . $name; if (!file_exists($file)) { file_put_contents($file, 'hh'); error_log(print_r($this->getClient()->getCluster(), true)); } */ if (empty($servers)) { $params = array('url' => $this->getClient()->getConfig('url'), 'host' => $this->getClient()->getHost(), 'port' => $this->getClient()->getPort(), 'path' => $this->getClient()->getConfig('path')); $response = $transport->exec($params); } else { // Set server id for first request (round robin by default) if (is_null(self::$_serverId)) { self::$_serverId = rand(0, count($servers) - 1); } else { self::$_serverId = (self::$_serverId + 1) % count($servers); } $server = $servers[self::$_serverId]; $response = $transport->exec($server); } return $response; }
/** * Sends request to server * * @return Elastica_Response Response object */ public function send() { $transport = $this->getTransport(); $servers = $this->getClient()->getConfig('servers'); if (empty($servers)) { $response = $transport->exec($this->getClient()->getHost(), $this->getClient()->getPort()); } else { // Set server id for first request (round robin by default) if (is_null(self::$_serverId)) { self::$_serverId = rand(0, count($servers) - 1); } else { self::$_serverId = (self::$_serverId + 1) % count($servers); } $server = $servers[self::$_serverId]; $response = $transport->exec($server['host'], $server['port']); } return $response; }