Skip to content

Instantly share code, notes, and snippets.

@timbuchwaldt
Created January 27, 2012 16:35
Show Gist options
  • Save timbuchwaldt/1689652 to your computer and use it in GitHub Desktop.
Save timbuchwaldt/1689652 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