Skip to content

examples

Lorna Jane Mitchell edited this page Aug 11, 2017 · 7 revisions

This is the place to come for more-than-API-docs. Check out the various sections to help with common tasks - and open an issue to share or request an example that isn't listed.

Fetch and display a list of documents

We can get all the documents from the database (NB fetching from views isn't yet supported). Each one arrives as a \PHPCouchDB\Document object with top-level properties that represent the values. If I try it on my shopping list app where every document has an "item" property, then the code looks like this:

<?php

require "vendor/autoload.php";

$server = new \PHPCouchDB\Server(["url" => "http://localhost:5984"]);
$my_db = $server->useDb(["name" => "shopping"]);

$docs = $my_db->getAllDocs();
foreach($docs as $doc) {
    echo "I am a doc, with item value: " . $doc->item . "\n";
}

I get output like this:

I am a doc, with item value: milk
I am a doc, with item value: bread
I am a doc, with item value: eggs

Additional Connection Requirements

If you need any additional configuration of the HTTP connection, then you can supply a "client" element to the constructor's array argument, and pass any \GuzzleHttp\ClientInterface object to that. Check the documentation of Guzzle Request Options as all of these can also be passed in the constructor of the \GuzzleHttp\Client.

<?php

require "vendor/autoload.php";

$client = new \GuzzleHttp\Client([
    "base_uri" => "http://localhost:5984"
    // set any other options here
]);

$server = new \PHPCouchDB\Server(["client" => $client]);
echo $server->getVersion();

Need Functionality That Isn't Supported

First of all: open an issue so we know what you looked for and missed.

Then, use \PHPCouchDB\Server::getClient to get the \GuzzleHTTP\Client. Since CouchDB is a pure HTTP interface, you can use the client and the excellent Guzzle documentation to do whatever it is you need.

Clone this wiki locally