/** * 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; }
/** * 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. */ 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'); } } } } # 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 Response object. return array($response_headers, $response_body); } catch (\Exception $e) { curl_close($ch); error_log('Error: ' . $e->getMessage()); return NULL; } }
/** * 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'; }
/** * Search a secondary index * @author Eric Stevens <*****@*****.**> * @param string $indexName - The name of the index to search * @param string $indexType - The type of index ('int' or 'bin') * @param string|int $startOrExact * @param string|int optional $end * @param bool $dedupe - whether to eliminate duplicate entries if any * @return array of Links */ public function indexSearch($indexName, $indexType, $startOrExact, $end = NULL, $dedupe = false) { $url = Utils::buildIndexPath($this->client, $this, "{$indexName}_{$indexType}", $startOrExact, $end, NULL); $response = Utils::httpRequest('GET', $url); $obj = new Object($this->client, $this, NULL); $obj->populate($response, array(200)); if (!$obj->exists()) { throw \Exception("Error searching index."); } $data = $obj->getData(); $keys = array_map("urldecode", $data["keys"]); $seenKeys = array(); foreach ($keys as $id => &$key) { if ($dedupe) { if (isset($seenKeys[$key])) { unset($keys[$id]); continue; } $seenKeys[$key] = true; } $key = new Link($this->name, $key); $key->client = $this->client; } return $keys; }