incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Caylor <dcay...@fh.org>
Subject Re: SIMPLE PHP EXAMPLE
Date Thu, 05 Aug 2010 20:17:16 GMT
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 <sam@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
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message