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 is compatible with both PHP 5.6 and 7.0.

Installing on Linux

For installation on Linux, install the couchbase-release repository, and then install the libcouchbase packages.
The following examples download and install couchbase-release repsitory, a C and C++ compiler, the C SDK development files (libcouchbase-devel [RPM] or libcouchbase-dev [DEB]), PHP development files, and finally the PHP SDK using pecl.

On Ubuntu 14.04 (Trusty), and possibly some other distributions, you will need to substitute for php-dev with php5-dev.
To find particular variants of php-dev for your Linux distribution, use the command

Installation on Mac OS X

As homebrew/php is no longer available, the current best way to install newer versions of PHP (and one with the necessary permissions) is found at https://php-osx.liip.ch.
Download install.sh from there or, if you are happy with the script, install in one line (in this example, PHP 7.2) with:

curl -s https://php-osx.liip.ch/install.sh | bash -s 7.2

Once you have PHP installed, then use PECL to install the Couchbase PHP SDK:

pecl install couchbase

Installing on Microsoft Windows

When using Microsoft Windows, download one of the pre-built binaries at Archives, matching your environment or at PECL.
You also have the option of downloading the source for the SDK and building it directly.

Post Installation - Setting up the php.ini

Once the PHP SDK has been installed, you need to specify that the PHP interpreter should load the Couchbase SDK as an extension.
To do this:

Locate the location of your php.ini file.
This can be done by php --ini

Insert the following line in the php.ini file; this should be in the [PHP] section.
If you don’t know where that is, simply search for existing commented or uncommented extension= entries in the file.

Linux and Mac OS

extension=couchbase.so

Windows

extension=couchbase.dll

The Couchbase SDK Version 2.2.4 depends on both the JSON and PCS (PHP Code Service) extensions.
Releases from 2.0.0 through 2.2.3 and from 2.3.0 forward depend only on the JSON extension.
Make sure that the load order is correct.
For example, if your distribution has just a single php.ini file, just insert the line after extension=json.so.
If your distribution uses a conf.d-style, name the file with the Couchbase SDK ini so that it will be alphabetically ordered after the JSON extension.

Because the extension depends on the C library (libcouchbase), the shared object libcouchbase.so or libcouchbase.dll has to be accessible by the PHP process when loading the extension.
On UNIX-like systems no additional steps are necessary, because the libcouchbase package installs shared objects into a common system location.
For Windows though, it must be copied into either into a location from the PATH for the PHP executable or into a directory with like executables (like apache2, IIS or php.exe).
This is controlled by your PHP distribution’s setup, so see its documentation for further information.

Information on new features, fixes, known issues as well as information on how to install older release versions is relnotes-php-sdk.adocl#_version_2_4_7_7_june_2018[in the release notes].

Hello Couchbase

The Hello Couchbase example consists of one PHP file, hello-couchbase.php.
The code opens a connection to Couchbase Server, retrieves a document, modifies the document, and stores the updated document in the database.
The hello-couchbase.php code is below.
Note that to connect to a Couchbase bucket, you must use Couchbase Role-Based Access Control (RBAC).
This is fully described in the section 6.0@server:security:security-authorization.adoc.
A username and password for the current user must be specified.
Following successful authentication, the bucket is opened.