You are viewing the documentation for a prerelease version.

View Latest

Install and Start Using the PHP SDK with Couchbase Server

The Couchbase PHP SDK allows you to connect to a Couchbase cluster from PHP. The Couchbase PHP SDK is a native PHP extension and uses the Couchbase high-performance C library to handle communicating to the cluster over Couchbase binary protocols.

These pages cover the Alpha of the Couchbase PHP SDK. As such they are likely to change without notice. This alpha code should not be used in production.

Documentation is incomplete, subject to change, and likely to contain broken links.

Installing the SDK

Hello Couchbase

Couchbase uses Role Based Access Control (RBAC) to control access to resources. Here we will use the Full Admin role created during installation of the Couchbase Data Platform. For production client code, you will want to use more appropriate, restrictive settings, but here we want to get you up and running quickly. If you’re developing client code on the same VM or machine as the Couchbase Server, your URI can be localhost.

$connectionString = "couchbase://localhost";
$cluster = new \Couchbase\Cluster($connectionString);

$cluster->authenticateAs("Administrator", "password");
// The bootstrap process and API is subject to change

Connection to the cluster is initialized by

// get a bucket reference
$bucket = $cluster->bucket("bucket-name");

If you installed the travel sample data bucket, substitute travel-sample for bucket-name.

// get a collection reference
$collection = $bucket->defaultCollection();

// or for named collection
$collection = $bucket->scope("myapp")->collection("my-collection");

The 3.0 SDK is ready for the introduction of Collections in Cheshire Cat, the next release of the Couchbase Data Platform. The latest release, Mad Hatter, brings a limited Developer Preview of Collections, allowing Documents to be grouped by purpose or theme, according to specified Scope. Here we will use the DefaultCollection, which covers the whole Bucket.

// upsert document
$upsertResult = $collection->upsert("my-document", ["name" => "mike"]);

// get document
$getResult = $collection->get("my-document");

KV Operations are described in detail on the KV Operations page. Now that you know the basics, you may wish to go straight to that page — or first see a complete worked example of using the Couchbase php client, our Travel Sample Application.

Additional Resources

The API reference is generated for each release and can be found here.

Couchbase welcomes community contributions to the PHP SDK. The PHP SDK source code is available on GitHub.