Is it that difficult to use php directly with couchdb that you need the added frameworks? I only work with php infrequently so I am no master php developer but it seems a little overly complicated considering that most Couchdb tutorials demonstrate the basics using curl. The websites I develop usually have some simple forms (guest book, or something similar) and simple retrieval mechanisms so I am looking for the simplest way to start using couchdb.
I am interested in learning about couchdb and how it solves problems differently or better than a sql server.
Thanks in advance for your input on the above questions and statements.
Paul
A sample of the usage (from my personal documentation for it) follows:
----
$couch = new SimpleCouch( array('first' => array('host'=>'couch.example.com',
'port'=>80, 'username'=>COUCH_USER, 'password'=>COUCH_PASSWORD) ) );
$first = $couch->first; //gives a SimpleCouchServer object, with basic
authentication at http://couch.example.com
$db = $first->db; //a SimpleCouchDatabase object
$db->doc; // returns the SimpleCouchDocument at couch.example.com/db/doc,
returns blank document if not existing
$doc->_( array("field1"=>"some data", "field2"=>"more data"),
"field3"=>true, "field4"=>325 );
// update with the given data
$doc->field1; //gives "some data"
$doc->field1 = "some new data"; // update the value of field1
unset( $doc->field1 );
// delete field1 locally
$doc->_( null, false ); //updates local copy from server data
unset( $doc->field1 ); // delete local field1
$doc->_( null, true ); // duplicates local data onto server version, exact
mirror
//effectively delete field1 on server
//convert $doc to a string to it as a JSON string, e.g.
echo $doc;
// gets '{"_id":"doc", "_rev":1, "field2": "more data", "field3":true,
"field4":325}' or similar
---
If you think that something like this would be useful to you, I will
endeavour to look through this code again ASAP and put it up on github.
Thanks.
David Pitman
I use PHP and CouchDB together nearly every day, so here are some quick
thoughts.
I would also recommend that you take a look at Sag (www.saggingcouch.com or
github.com/ravidgemole/sag), the PHP library I wrote for CouchDB; not that I'm
biased or anything.
On Mon, Aug 02, 2010 at 12:00:08PM -0700, Paul J Barrett wrote:
> I see many examples of using some library api to use php with couchdb, but I
> don't see anything that demonstrates using json and making the put, post,
> get, delete directly using the REST interface.
In PHP we do 99% of our JSON work with json_encode() and json_decode(). Some
PHP implementations hide this from their users, but they're really just using
those two functions behind the OOP curtain.
> Is it that difficult to use php directly with couchdb that you need the added
> frameworks?
Nope, it just makes it easier so that you don't have to worry about configuring
cURL, formatting packets, or dealing with streams/sockets. This is a language
agnostic consideration. Ex., you wouldn't want to implement AJAX yourself for
JavaScript - you would use a library like jQuery.
Side note, I'm not sure if, or where, you're drawing the line between framework
and library.
> I only work with php infrequently so I am no master php developer
> but it seems a little overly complicated considering that most Couchdb
> tutorials demonstrate the basics using curl.
If you like cURL, then I'd recommend looking at PHP's library www.php.net/curl.
If you're on a Debian based Linux distro, then `sudo apt-get install php5-curl`
will do the trick.
Also, the reason so many tutorials use cURL for demonstration is that it's
language agnostic: you don't need to know the author's language of choice to
understand the tutorial. And using an actual program is better than just
showing HTTP packets, because the reader might not know enough about the
protocol and they can simply re-run the examples (can't really "re-run" a
packet).
> The websites I develop usually have some simple forms (guest book, or
> something similar) and simple retrieval mechanisms so I am looking for the
> simplest way to start using couchdb.
Again, take a look at Sag - I try to build it with relaxation and simplicity.
And yes, there are other libraries that you can try too.
Cheers,
--
Sam Bisbee
www.sbisbee.com
public function getDoc( $_id, $db = false, $json = false ) {
$db = ( !empty( $db ) ) ? $db : $this->db;
if( empty( $_id ) ) return '';
$client = new Zend_Http_Client( );
$resp = $client->setUri( "{$this->URI}/$db/$_id" )
->request( 'GET' );
if( $resp->getStatus( ) == 200 ){
if($json){
return $resp->getBody( );
} else {
return Zend_Json::decode( $resp->getBody( ) );
}
} else{
$message = "Fh_Db_Couchdb: Failed to load $_id from $db.";
trigger_error( $message );
return false;
}
}
Zend_Json::decode basically is the same as using json_decode with
$assoc=true (the second parameter to json_decode) so you get an array rather
than object back. Don't use this method just like this without your own
filtering and so on, but I think this conveys the idea.
David Caylor