Install and Start Using the PHP SDK with Couchbase Server
The Couchbase PHP SDK allows you to connect to a Couchbase cluster from PHP. It 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.x 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 7.0).
The Couchbase PHP SDK is compatible with PHP 7.3+.
Installing the SDK
The Couchbase PHP SDK depends on the C SDK — libcouchbase (LCB); version 3.2.0 or higher is required for PHP Client 3.2.
On Linux and MacOS, libcouchbase
is installed separately as a system library.
On Windows systems libcouchbase
is included in our binary distribution.
On macOS and most Linux systems you can simply install libcouchbase
and then use
the pecl
command to install the Couchbase PHP SDK.
$ pecl install https://packages.couchbase.com/clients/php/couchbase-3.2.2.tgz
Alpine Linux is very slim and uses musl libc
and the apk
package manager.
As a result, the installation is a little different from other Unix-Like systems and pecl
equivalent packages are used instead.
Using apk
, you would install your preferred php
version, install libcouchbase
,
and then install the equivalent pecl
package for the version of php
that you’re using.
$ apk add php7
$ apk add libcouchbase
$ apk add php7-pecl-couchbase
On Windows systems the version of libcouchbase
verified at release is included in the distribution.
Simply download the correct version according to your installation details,
and then copy the corresponding libcouchbase.dll
and php_couchbase.dll
binaries into the extension_dir
as configured in your php.ini
configuration.
$ copy /b libcouchbase.dll C:\php\ext
$ copy /b php_couchbase.dll C:\php\ext
Refer to the additional details on the Release Notes & Archives page.
In particular, follow the post-install instructions according to the .ini
practices for your platform.
Also notice that the Couchbase PHP SDK depends on the JSON extension,
and it must 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 Capella 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 default collection reference
$collection = $bucket->defaultCollection();
// or for named collection
$collection = $bucket->scope("myapp")->collection("my-collection");
The 3.2 SDK supports full integration with the Collections feature in the latest release of the Couchbase Data Platform, Couchbase Server 7.0.
This brings complete support of Collections, allowing Documents to be grouped by purpose or theme, according to a specified Scope.
Here we will use the users
collection within the tenant_agent_00
scope from travel-sample
bucket as an example.
$scope = $bucket->scope("tenant_agent_00");
$collection = $agentScope->collection("users");
The code shows how you would use a named collection and scope. A named or default collection will provide the same functionality as bucket-level operations did in previous versions of Couchbase Server.
The defaultCollection
must be used when connecting to a 6.6 cluster or earlier.
// 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 Capella, be sure to get the correct endpoint as well as user, password, and couchbasecloudbucket
For developing on Capella, if you are not working from the same Availability Zone, refer to the following:
-
Notes on Constrained Network Environments,
-
If you have a consumer-grade router which has problems with DNS-SRV records review our Troubleshooting Guide.
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.