Fork me on GitHub The Couchbase PHP SDK allows you to connect to a Couchbase cluster from PHP. It is a native PHP extension and uses Couchbase's high performance C library to handle communicating to the cluster over Couchbase’s binary protocols.


To connect to a bucket, simply instantiate a new CouchbaseCluster object to represent your cluster, and call the openBucket() method, passing in the name of the bucket that you want to connect to. If no bucket name is specified, the default bucket is opened. The following example shows how to connect to a bucket on localhost:

$myCluster = new CouchbaseCluster('couchbase://localhost');
$myBucket = $myCluster->openBucket('default');


In most cases, operations are performed by invoking the relevant method whilst passing the key, any data and meta information that goes with it. The operation will return the result of the operation, or alternatively throw an Exception if an error occured.

Here is an example of performing a get operations:

$res = $myBucket->get('document_name');
echo 'Value: ' . $res->value;

Additionally, sometimes more information beyond the key is needed for an operation, as seen here:

$myBucket->insert('document_name', array('some'=>'value'));

Performing Queries

In addition to the basic operations that are available, we additional provide methods to perform queries against your data. These queries allow you to ask questions like "What beers start with A?" rather than simply "Give me the beer that is called Aaas_Frankl". This is possible through the use of Couchbase's map-reduce views engine. The following is an example of performing a view query.

$query = CouchbaseViewQuery::from('beer', 'by_name')->skip(6)->limit(3);;
$results = $myBucket->query($query);