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.

    The Couchbase PHP SDK 3.0 is a complete rewrite of the API, reducing the number of overloads to present a simplified surface area, and adding support for future Couchbase Server features like Collections and Scopes (available in Couchbase Server 6.5 as a developer preview).

    The Couchbase PHP SDK is compatible with PHP 7.2+.

    Installing the SDK

    Before installing the PHP SDK, you must install the C SDK — libcouchbase (LCB); version 3.0 or higher is required from PHP Client 3.0.

    After installing the C SDK, install the PHP SDK through your PHP distribution’s pecl command:

    $ pecl install https://packages.couchbase.com/clients/php/couchbase-3.0.4.tgz

    Further details can be found on the Release Notes & Archives page. In particular, look at the post-install instructions in light of the .ini practices for your platform, and note that the Couchbase SDK depends on the JSON module, therefore that module has to be loaded before 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";
    $options = new \Couchbase\ClusterOptions();
    $options->credentials("Administrator", "password");
    $cluster = new \Couchbase\Cluster($connectionString, $options);

    The Cluster provides access to cluster-level operations like N1Ql queries, analytics, or full-text search. You will also find different management APIs on it.

    If you are connecting to Couchbase Cloud rather than a local Couchbase Server, then also refer to the Cloud section, below.

    To access the KV (Key/Value) API or to query views, you need to open a Bucket:

    // 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 an upcoming release of the Couchbase Data Platform. Couchbase Server 6.5 brings a limited Developer Preview of Collections, allowing Documents to be grouped by purpose or theme, according to a specified Collection. Here we’ve used the DefaultCollection, which provides the same functionality as bucket-level operations did in previous versions of Couchbase Server.

    // 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.

    Cloud Connections

    If you are connecting to Couchbase Cloud, be sure to get the correct endpoint as well as user, password, and couchbasecloudbucket

    For developing on Couchbase Cloud, if you are not working from the same Availability Zone, refer to the following:

    Additional Resources

    The API reference is generated for each release and can be found here. Older API references are linked from their respective sections in the Release Notes.

    The Migrating from SDK2 to 3 page highlights the main differences to be aware of when migrating your code.

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