Exemplo n.º 1
0
 /**
  * Makes calls to the elasticsearch server
  *
  * All calls that are made to the server are done through this function
  *
  * @param string $host Host name
  * @param int $port Port number
  * @return Elastica_Response Response object
  */
 public function exec(array $params)
 {
     $conn = $this->_getConnection();
     $request = $this->getRequest();
     // If url is set, url is taken. Otherwise port, host and path
     if (!empty($params['url'])) {
         $baseUri = $params['url'];
     } else {
         if (!isset($params['host']) || !isset($params['port'])) {
             throw new Elastica_Exception_Invalid('host and port have to be set');
         }
         $path = isset($params['path']) ? $params['path'] : '';
         $baseUri = $this->_scheme . '://' . $params['host'] . ':' . $params['port'] . '/' . $path;
     }
     $baseUri .= $request->getPath();
     curl_setopt($conn, CURLOPT_URL, $baseUri);
     curl_setopt($conn, CURLOPT_TIMEOUT, $request->getConfig('timeout'));
     curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($conn, CURLOPT_CUSTOMREQUEST, $request->getMethod());
     curl_setopt($conn, CURLOPT_FORBID_REUSE, 0);
     $this->_setupCurl($conn);
     $headersConfig = $request->getConfig('headers');
     if (!empty($headersConfig)) {
         $headers = array();
         while (list($header, $headerValue) = each($headersConfig)) {
             array_push($headers, $header . ': ' . $headerValue);
         }
         curl_setopt($conn, CURLOPT_HTTPHEADER, $headers);
     }
     // TODO: REFACTOR
     $data = $request->getData();
     if (isset($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);
     $responseString = curl_exec($conn);
     $end = microtime(true);
     // Checks if error exists
     $errorNumber = curl_errno($conn);
     $response = new Elastica_Response($responseString);
     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;
 }
Exemplo n.º 2
0
 /**
  * Loads all data into the results object (initalisation)
  *
  * @param Elastica_Response $response Response object
  */
 protected function _init(Elastica_Response $response)
 {
     $this->_response = $response;
     $result = $response->getData();
     $this->_totalHits = $result['hits']['total'];
     if (isset($result['hits']['hits'])) {
         foreach ($result['hits']['hits'] as $hit) {
             $this->_results[] = new Elastica_Result($hit);
         }
     }
 }
Exemplo n.º 3
0
 /**
  * Makes calls to the elasticsearch server
  *
  * @param  array             $params Host, Port, ...
  * @return Elastica_Response Response object
  */
 public function exec(array $params)
 {
     $request = $this->getRequest();
     $memcache = new Memcache();
     $memcache->connect($params['host'], $params['port']);
     // Finds right function name
     $function = strtolower($request->getMethod());
     $data = $request->getData();
     $content = '';
     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);
     }
     $responseString = '';
     switch ($function) {
         case 'post':
         case 'put':
             $memcache->set($request->getPath(), $content);
             break;
         case 'get':
             $responseString = $memcache->get($request->getPath() . '?source=' . $content);
             echo $responseString . PHP_EOL;
             break;
         case 'delete':
             break;
         default:
             throw new Elastica_Exception_Invalid('Method ' . $function . ' is not supported in memcache transport');
     }
     $response = new Elastica_Response($responseString);
     if (defined('DEBUG') && DEBUG) {
         $response->setQueryTime($end - $start);
         $response->setTransferInfo(curl_getinfo($conn));
     }
     if ($response->hasError()) {
         throw new Elastica_Exception_Response($response);
     }
     return $response;
 }
Exemplo n.º 4
0
 /**
  * @param Elastica_Response $response
  */
 public function __construct(Elastica_Response $response)
 {
     $this->_response = $response;
     parent::__construct($response->getError());
 }
Exemplo n.º 5
0
 /**
  * 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;
 }