class RavenDB { var $server; var $database; var $pem; function __construct($server, $database, $pem = NULL) { $this->server = $server; $this->database = $database; $this->pem = $pem; } function put($id, $doc) { $url = $this->_url("/docs?id=" . $id); $body = json_encode($doc); return $this->_exec("PUT", $url, 201, $body); } function get($id) { $url = $this->_url("/docs?id=" . $id); return $this->_exec("GET", $url, 200, NULL)->Results[0]; } function query($query, $args = NULL) { $r = $this->raw_query($query, $args); return $r->Results; } function raw_query($query, $args = NULL) { $url = $this->_url("/queries"); $body = json_encode(array("Query" => $query, "QueryParameters" => $args)); return $this->_exec("POST", $url, 200, $body); } function del($id) { $url = $this->_url("/docs?id=" . $id); $this->_exec("DELETE", $url, 204, NULL); } private function _exec($method, $url, $expectedStatusCode, $body) { $curl = curl_init($url); try{ curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, '2'); /// CURLOPT_SSL_VERIFYHOST // 1 to check the existence of a common name in the SSL peer certificate. // 2 to check the existence of a common name and also verify that it matches the hostname provided. // 0 to not check the names. // In production environments the value of this option should be kept at 2 (default value). // Support for value 1 removed in cURL 7.28.1. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, '1'); // CURLOPT_SSL_VERIFYPEER // false to stop cURL from verifying the peer's certificate. // Alternate certificates to verify against can be specified with the // CURLOPT_CAINFO option or a certificate directory can be specified with the CURLOPT_CAPATH option. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); // CURLOPT_CUSTOMREQUEST // A custom request method to use instead of "GET" or "HEAD" when doing a HTTP request. // This is useful for doing "DELETE" or other, more obscure HTTP requests. // Valid values are things like "GET", "POST", "CONNECT" and so on; i.e. // Do not enter a whole HTTP request line here. For instance, entering "GET /index.html HTTP/1.0\r\n\r\n" would be incorrect. curl_setopt($curl, CURLOPT_SSLCERT, $this->pem); // CURLOPT_SSLCERT The name of a file containing a PEM formatted certificate. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // CURLOPT_RETURNTRANSFER // A true to return the transfer as a string of the return value of curl_exec() instead of outputting it directly. if($body != NULL){ curl_setopt($curl, CURLOPT_POSTFIELDS, $body); } $response = curl_exec($curl); if (!curl_errno($curl)) { switch ($http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE)) { case $expectedStatusCode: return json_decode($response); case 404: return NULL; default: echo $response; throw new Exception( $url . " GOT " . $http_code . " - " . $response); } } } finally{ curl_close($curl); } } private function _url($path) { return $this->server . "/databases/" . $this->database . $path; } }