You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Paul J Barrett <Pa...@lvac.com> on 2010/08/02 21:00:08 UTC

SIMPLE PHP EXAMPLE

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.

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

Re: SIMPLE PHP EXAMPLE

Posted by David Caylor <dc...@fh.org>.
I use CouchDb with Zend Framework and the Zend_Http_Client.  Here's
basically my method for getting a document from CouchDB:

    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

On Wed, Aug 4, 2010 at 12:39 PM, Sam Bisbee <sa...@sbisbee.com> wrote:

> Hi Paul,
>
> 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.comor
> 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
>

Re: SIMPLE PHP EXAMPLE

Posted by Sam Bisbee <sa...@sbisbee.com>.
Hi Paul,

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

Re: SIMPLE PHP EXAMPLE

Posted by David Pitman <da...@thinktree.org>.
Not exactly sure what you're after here, but I had a similar thought that
the existing php classes for dealing with couchdb are a bit heavyweight, so
a little while ago I wrote myself a nice simple one.  I haven't put it up on
github yet because it doesn't support couchdb with any features introduced
since about 0.8 (not "shows", "lists", no oauth authentication, etc.) but it
has helped me in the meantime with a couple of projects.  I haven't touched
this code since about a year ago, which is why it is out of date as regards
couchdb features support.

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


On Tue, Aug 3, 2010 at 5:00 AM, Paul J Barrett <Pa...@lvac.com> 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.
>
> 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