PHP CouchDB Extension

This PHP extension wraps the CouchDB RESTful API, allowing you to access CouchDB servers from within your PHP scripts with easy to use functions. The extension does not encode or decode the JSON response's or requests, you need to do that using the PHP json extension (Built in Support for JSON encoding and decoding is in my svn version only at the moment and requires PHP ⇒ 5.2.10). This software is still alpha please use with care, you have been warned.

Download

Installation

Using pecl command

  • Download the source
  • run the command “pecl install couchdb-0.0.1a.tgz”
  • Add “extension=couchdb.so” to your php.ini file

Using sources

  • Download the source
  • Unpack the tarball tar xzvf couchdb-0.0.1a.tgz
  • Enter the source directory and run “phpize && ./configure && make && make install”
  • Add “extension=couchdb.so” to your php.ini file.

Build into PHP

  • Download the source
  • Unpack the tarball to the PHP extensions dir “tar xzvf couchdb-0.0.1a.tgz -C $PHP_SOURCE/ext/”
  • Reconfigure the PHP source “rm configure && ./buildconf –force”
  • Configure php with ”./configure –enable-couchdb”

Use

The following functions are currently implemented. The functions that return strings, return a JSON encoded string you will need to decode this to an array or PHP object using the PHP json extension functions.

void couchdb_client::__construct(string url [,string db_name])
bool couchdb_client::selectDB(string db_name)
string couchdb_client::listDatabases(void)
string couchdb_client::getDatabaseInfo([, string db_name ])
string couchdb_client::getDatabaseChanges([, string db_name ])
bool couchdb_client::compactDatabase([, string db_name ])
bool couchdb_client::createDatabase([, string db_name ])
bool couchdb_client::deleteDatabase([, string db_name])
string couchdb_client::getLastResponse()
string couchdb_client::getAllDocs()
string couchdb_client::getAllDocsBySeq()
string couchdb_client::getDoc(string docid)
string couchdb_client::storeDoc(string docid [,array params])
string couchdb_client::deleteDoc(string docid, array params)
string couchdb_client::copyDoc(string docid, string newdocid)
bool couchdb_client::setCAPath(string ca_path, string ca_info)
array couchdb_client::getCAPath(void)

Example

The following example is included in the source tarball. The image to be attached (php.gif) should be in the same directory.

<?php
$conn = new couchdb_client("http://localhost:5984");
print "Creating Database andrew :";
if($conn->createDatabase("andrew"))
print "PASS\n";
else
print "FAIL\n";
 
echo "Getting Database list\n";
$result = $conn->listDatabases();
print_r(json_decode($result,true));
print "===========================\n";
print "Selecting Database andrew\n";
$conn->selectDB("andrew");
print "===========================\n";
print "Getting Database info\n";
$result = $conn->getDatabaseInfo();
print_r(json_decode($result,true));
print "===========================\n";
print "Store document to DB\n";
$result = $conn->storeDoc("first_doc",'{"Subject":"I like Plankton","Author":"Rusty","PostedDate":"2006-08-15T17:30:12-04:00","Tags":["plankton", "ba
seball", "decisions"],"Body":"I decided today that I don\'t like baseball. I like plankton."}');
print_r(json_decode($result,true));
print "===========================\n";
 
print "Store attachment to DB\n";
$arr = array();
$google=file_get_contents('php.gif');
$result = $conn->storeAttachment("andrew_interview",$google,"php.gif","image/gif");
print_r(json_decode($result,true));
print "===========================\n";
print "Copy document to another document\n";
$result = $conn->copyDoc("first_doc","second_doc");
$r = json_decode($result,true);
print_r($r);
print "===========================\n";
print "Get all documents in DB\n";
$result = $conn->getAllDocs();
print_r(json_decode($result,true));
print "===========================\n";
print "Delete a document\n";
$arr = array("rev"=>$r['rev']);
$result = $conn->deleteDoc("second_doc",$arr);
print_r(json_decode($result));
print "===========================\n";
print "Compacting Database :";
if($conn->compactDatabase())
print "PASS\n";
else
print "FAIL\n";
 
print "Waiting for compaction to finish :";
sleep(5);
print "DONE\n";
print "===========================\n";
print "Getting updated Database info\n";
$result = $conn->getDatabaseInfo();
print_r(json_decode($result,true));
print "===========================\n";
print "Delete the database :";
if($conn->deleteDatabase())
print "PASS\n";
else
print "FAIL\n";
 
unset($conn);
?>
php_couchdb_extension-0_0_1.txt · Last modified: 2009/10/23 18:03 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki