Created
January 27, 2012 16:35
-
-
Save timbuchwaldt/1689652 to your computer and use it in GitHub Desktop.
Minimal PHP CouchDB Layer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/* | |
* 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