/**
  * Send HTTP request
  *
  * Given a Method, URL, Headers, and Body, perform and HTTP request,
  * and return an array of arity 2 containing an associative array of
  * response headers and the response body.
  *
  * @return array
  */
 public static function httpRequest($method, $url, $request_headers = array(), $obj = '')
 {
     # Set up curl
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
     if ($method == 'GET') {
         curl_setopt($ch, CURLOPT_HTTPGET, 1);
     } else {
         if ($method == 'POST') {
             curl_setopt($ch, CURLOPT_POST, 1);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $obj);
         } else {
             if ($method == 'PUT') {
                 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
                 curl_setopt($ch, CURLOPT_POSTFIELDS, $obj);
             } else {
                 if ($method == 'DELETE') {
                     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
                 } else {
                     if ($method == 'HEAD') {
                         curl_setopt($ch, CURLOPT_NOBODY, 1);
                     }
                 }
             }
         }
     }
     # Capture the response headers...
     $response_headers_io = new StringIO();
     curl_setopt($ch, CURLOPT_HEADERFUNCTION, array(&$response_headers_io, 'write'));
     # Capture the response body...
     $response_body_io = new StringIO();
     curl_setopt($ch, CURLOPT_WRITEFUNCTION, array(&$response_body_io, 'write'));
     try {
         # Run the request.
         curl_exec($ch);
         $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
         curl_close($ch);
         # Get the headers...
         $parsed_headers = Utils::parseHttpHeaders($response_headers_io->contents());
         $response_headers = array("http_code" => $http_code);
         foreach ($parsed_headers as $key => $value) {
             $response_headers[strtolower($key)] = $value;
         }
         # Get the body...
         $response_body = $response_body_io->contents();
         # Return a new RiakResponse object.
         return array($response_headers, $response_body);
     } catch (Exception $e) {
         curl_close($ch);
         error_log('Error: ' . $e->getMessage());
         return null;
     }
 }
 /**
  * Retrieve a sibling by sibling number.
  *
  * @param  integer $i - Sibling number.
  * @param  integer $r - R-Value. Wait until this many partitions
  * have responded before returning to client.
  * @return Object.
  */
 public function getSibling($i, $r = null)
 {
     # Use defaults if not specified.
     $r = $this->bucket->getR($r);
     # Run the request...
     $vtag = $this->siblings[$i];
     $params = array('r' => $r, 'vtag' => $vtag);
     $url = Utils::buildRestPath($this->client, $this->bucket, $this->key, null, $params);
     $response = Utils::httpRequest('GET', $url);
     # Respond with a new object...
     $obj = new Object($this->client, $this->bucket, $this->key);
     $obj->jsonize = $this->jsonize;
     $obj->populate($response, array(200));
     return $obj;
 }
 /**
  * Check if a given key exists in a bucket
  *
  * @author Edgar Veiga <*****@*****.**>
  * @param string $key - The key to check
  * @return bool
  */
 public function hasKey($key)
 {
     $url = Utils::buildRestPath($this->client, $this, $key);
     $response = Utils::httpRequest('HEAD', $url);
     if ($response == null) {
         throw new Exception("Error checking if key exists.");
     }
     $status = $response[0]['http_code'];
     if ($status === 200) {
         return true;
     }
     return false;
 }
 /**
  * Check if the Riak server for this Client is alive
  *
  * @return boolean
  */
 public function isAlive()
 {
     $url = 'http://' . $this->host . ':' . $this->port . '/ping';
     $response = Utils::httpRequest('GET', $url);
     return $response != null && $response[1] == 'OK';
 }
 /**
  * Run the map/reduce operation
  *
  * Returns an array of results, or an
  * array of Link objects if the last phase is a link phase.
  *
  * @param integer $timeout - Timeout in seconds.
  * @return array()
  */
 public function run($timeout = null)
 {
     $num_phases = count($this->phases);
     $linkResultsFlag = false;
     # If there are no phases, then just echo the inputs back to the user.
     if ($num_phases == 0) {
         $this->reduce(array("riak_kv_mapreduce", "reduce_identity"));
         $num_phases = 1;
         $linkResultsFlag = true;
     }
     # Convert all phases to associative arrays. Also,
     # if none of the phases are accumulating, then set the last one to
     # accumulate.
     $keep_flag = false;
     $query = array();
     for ($i = 0; $i < $num_phases; $i++) {
         $phase = $this->phases[$i];
         if ($i == $num_phases - 1 && !$keep_flag) {
             $phase->keep = true;
         }
         if ($phase->keep) {
             $keep_flag = true;
         }
         $query[] = $phase->to_array();
     }
     # Add key filters if applicable
     if ($this->input_mode == 'bucket' && count($this->key_filters) > 0) {
         $this->inputs = array('bucket' => $this->inputs, 'key_filters' => $this->key_filters);
     }
     # Add index search if applicable
     if ($this->input_mode == 'bucket' && count($this->index) > 0) {
         $this->inputs = array_merge(array('bucket' => $this->inputs), $this->index);
     }
     # Construct the job, optionally set the timeout...
     $job = array("inputs" => $this->inputs, "query" => $query);
     if ($timeout != null) {
         $job["timeout"] = $timeout;
     }
     $content = json_encode($job);
     # Do the request...
     $url = "http://" . $this->client->host . ":" . $this->client->port . "/" . $this->client->mapred_prefix;
     $response = Utils::httpRequest('POST', $url, array('Content-type: application/json'), $content);
     $result = json_decode($response[1]);
     # If the last phase is NOT a link phase, then return the result.
     $linkResultsFlag |= end($this->phases) instanceof LinkPhase;
     # If we don't need to link results, then just return.
     if (!$linkResultsFlag) {
         return $result;
     }
     # Otherwise, if the last phase IS a link phase, then convert the
     # results to Link objects.
     $a = array();
     foreach ($result as $r) {
         $tag = isset($r[2]) ? $r[2] : null;
         $link = new Link($r[0], $r[1], $tag);
         $link->client = $this->client;
         $a[] = $link;
     }
     return $a;
 }