couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <>
Subject [Couchdb Wiki] Update of "GettingStartedWithPhp" by NoahSlater
Date Wed, 26 Mar 2008 23:31:56 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.

The following page has been changed by NoahSlater:

The comment on the change is:
Copied from the original CouchDB wiki

New page:
Getting started with PHP and the CouchDB API.

== Setup ==

To get this example code running you need to install CouchDB on your system and have it running
on port 5984. If you use a different machine or port, change the first two lines of code to
your specific values.

== What Does it Do? ==

This example first creates a !CouchSimple object that we're going to use for making connections
to CouchDB on our machine, port 5984. Notice that this response is already encoded in JSON.
CouchDB returns nothing but JSON wrapped in HTTP responses.

It then tries to make a simple GET request to the root of the data store.

We then look for a list of all databases in CouchDB and we find the one that ships with it,
the ''test_duite_db''. CouchDB has all sorts of special URLs for specific tasks. The ''/_all_dbs''
URL gives us a list of all the databases.

Next, we create a new database ''test'' using the HTTP method ''PUT''. Creating databases
is that easy.

Then we have a look all the documents in a database. To do so we send a GET request to the
special URL ''/test/_all_docs''. We get an empty list.

Time for action. We are now storing actual data, wrapped in JSON, in CouchDB. To do so, we
make POST request to the test database and include a JSON object that has the special attribute
''"_id": "123"''. This is unique identifier each document in CouchDB has. If you don't specify
one here, CouchDB does it for you. In the response you see, that CouchDB then tells you what
''_id'' was created. There is also the ''_rev'' attribute which can be used to specify a document's
revision, but this is beyond the scope of this introduction.

Assuming all goes well, we have another look at all the documents in the test database and
'lo and behold, our document is in.

We then proceed to getting it back out again with a simple GET request to ''/test/123''. All
documents you put into CouchDB can be retrieved like this. With their database and ''_id''
as the URL.

At last, we delete our database. The HTTP DELETE method does the job. You can also DELETE
single documents in the same way.

 $options['host'] = "localhost";
 $options['port'] = 5984;

 $couch = new CouchSimple($options);

 // See if we can make a connection
 $resp = $couch->send("GET", "/");
 // response: string(46) "{"couchdb": "Welcome", "version": "0.7.0a553"}"

 // Get a list of all databases in CouchDb
 $resp = $couch->send("GET", "/_all_dbs");
 // response: string(17) "["test_suite_db"]"

 // Create a new database "test"
 $resp = $couch->send("PUT", "/test");
 // response:
 // string(12) "{"ok":true}
 // "

 // Get all documents in that database
 $resp = $couch->send("GET", "/test/_all_docs");
 // response:
 // string(27) "{"total_rows":0,"rows":[]}
 // "

 // Create a new document in the database test with the id 123 and some data
 $resp = $couch->send("PUT", "/test/123", '{"_id":"123","data":"Foo"}');
 // response:
 // string(42) "{"ok":true,"id":"123","rev":"2039697587"}
 // "

 // Get all documents in test again, seing doc 123 there
 $resp = $couch->send("GET", "/test/_all_docs");
 // response:
 // string(91) "{"total_rows":1,"offset":0,"rows":[{"id":"123","key":"123","value":{"rev":"2039697587"}}]}

 // Get back document with the id 123
 $resp = $couch->send("GET", "/test/123");
 // response:
 // string(47) "{"_id":"123","_rev":"2039697587","data":"Foo"}
 // "

 // Delete our "test" database
 $resp = $couch->send("DELETE", "/test/");
 // response: string(12) "{"ok":true}

 class CouchSimple
     function CouchSimple($options)
         foreach($options AS $key => $value) {
             $this->$key = $value;

     function send($method, $url, $post_data = NULL)
         $s = fsockopen($this->host, $this->port, $errno, $errstr);

         if(!$s) {
             echo "$errno: $errstr\n";
             return false;

         $request = "$method $url HTTP/1.0\r\nHost: localhost\r\n";

         if($post_data) {
             $request .= "Content-Length: ".strlen($post_data)."\r\n\r\n";
             $request .= "$post_data\r\n";
         } else {
             $request .= "\r\n";
         fwrite($s, $request);

         $response = "";
         while(!feof($s)) {
             $response .= fgets($s);

         list($this->headers, $this->body) = explode("\r\n\r\n", $response);
         return $this->body;


View raw message