Skip to content

Instantly share code, notes, and snippets.

@3l3n01
Forked from timbuchwaldt/couch.php
Created November 20, 2012 00:06
Show Gist options
  • Save 3l3n01/4115036 to your computer and use it in GitHub Desktop.
Save 3l3n01/4115036 to your computer and use it in GitHub Desktop.
Minimal PHP CouchDB Layer
<?
/*
* Usage:
* Instantiate class $c = couch::i();
* You can now use $c to make calls
*/
class couch
{
private static $instance = null;
private $curl_object = null;
private $domain = ''; #Your Domain
private $port = '5984'; #Default Port
private $database = ''; #Database
private $username = ''; #Username
private $password = ''; #Password
public static function i(){
if(self::$instance === null)
self::$instance = new self;
return self::$instance;
}
public function make_call($url,$method,$message) {
$t_url = $this->username':'.$this->password.'@'.$this->domain.':'.$this->port.'/'.$this->database.'/'.$url;
curl_setopt($this->curl_object, CURLOPT_URL,$t_url);
curl_setopt($this->curl_object, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($this->curl_object, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->curl_object, CURLOPT_POSTFIELDS, $message);
curl_setopt($this->curl_object, CURLOPT_HTTPHEADER,array('Content-Type: application/json; charset=utf-8'));
$result=curl_exec ($this->curl_object);
$result = json_decode($result,false);
return $result;
}
public function do_put($url,$message=array()){
return $this->make_call($url,"PUT",$message);
}
public function do_post($url,$message=array()){
return $this->make_call($url,"POST",$message);
}
public function do_get($url,$message=array()){
return $this->make_call($url,"GET",$message);
}
public function do_delete($url,$message=array()){
return $this->make_call($url,"DELETE",$message);
}
private function __construct(){
$this->curl_object=curl_init();
}
private function __clone(){}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment