Setting Up Couchbase PHP SDK with VSCode

    +
    Discover how to get up and running developing applications with the Couchbase PHP SDK 4.0+ using Visual Studio Code.

    A simple PHP orientation intro for non-PHP folk who are evaluating the Couchbase PHP SDK.

    Is This Page for You?

    This page is to help evaluate the Couchbase PHP SDK, if PHP is not where you spend the majority of your working day. It is aimed at Software Architects, QE folk, managers, and anyone else who needs to run through using the PHP SDK without necessarily being comfortable with the PHP environment. If this is not you, head back to the rest of the Couchbase PHP SDK documentation.

    Installing PHP and the Couchbase PHP SDK

    The Couchbase PHP SDK generally tracks PHP’s own supported versions, and recommends the most recent "Active Support" version, except as specified in our documentation and especially the release notes.

    As of April 2022, these versions are PHP 8.1 (Latest Active Support), 8.0 (Active Support), and 7.4 (Security Support).

    In this section, we’ll recommend a known good installation path here for the purposes of evaluating Couchbase. Though we’ll call out some key sections where you may wish to override that for your needs, you and your in-house PHP experts will want to make a decision for ongoing development and deployment.

    The PHP install page has full details and many options for installing PHP on various operating systems.

    • Mac

    • Linux

    • Windows

    Install PHP using Homebrew. Once you have installed PHP, simply use pecl to install the Couchbase PHP SDK.

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

    https://www.php.net/manual/en/install.unix.php has information on installing on various Linux systems. Where there are no specific instructions on the official PHP page, or in our tested guidance below, it is worth consulting your distro’s trusted documentation, as there may be known-good repositories with PHP binaries to evaluate.

    Once you have installed PHP, simply use pecl to install the Couchbase PHP SDK.

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

    On Debian, you can do:

    $ apt install php php-common php-cli
    $ pecl install https://packages.couchbase.com/clients/php/couchbase-4.0.0.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.

    $ apk add php7
    $ apk add php7-pecl-couchbase

    Windows downloads for PHP are available from https://windows.php.net/download/ A handy sidebar on the left explores the various considerations you will want to consider. For most environments, the first option (PHP 8.1 VS16 x64 Non Thread Safe) should be fine for evaluation purposes.

    Windows downloads and instructions for the PHP SDK are available from the installation page.

    Next Steps

    If you’re just starting with PHP then the official PHP Manual is a great resource.

    Using a Code Editor (Visual Studio Code)

    Visual Studio Code is a free code editor which runs on Windows, Linux and MacOS and can be downloaded here. Once downloaded, follow the installation details for the relevant platform:

    we’ve given instructions for VS Code as it’s a currently popular, cross-platform, multi-language editor that’s seeing widespread use, and is easy to set up and get started. If you’re planning to primarily develop in PHP, you may prefer to look into using a full IDE like PhpStorm.

    Adding PHP Development Support

    VSCode is a flexible editor, with support for various programming languages. Though basic syntax highlighting for PHP is included in the box, you’ll find it useful to add an extension with support for development — debugging, discovery, and navigation — in your chosen programming language.

    For PHP, we suggest using PHP Intelephense.

    1. You can install from within VSCode itself:

      • Open VSCode

        • Select the Extensions button on the left hand side.

        • Type bmewburn.vscode-intelephense-client into the Search Extensions in Marketplace textbox and hit enter.

        • Select and install the language extension into the editor.

    2. Alternatively, use the VSCode marketplace:

    Adding the code command

    If you work from the command-line, you’ll want to add the code command to allow you to edit a file directly.

    In VSCode, View the Command Palette (Ctrl+Shift+P or Cmd+Shift+P) and search for Shell command: Install 'code' command in PATH and press Enter.

    You can now type code MyExample.php to open a single file in VSCode, or code . to view the current directory.

    Creating a project

    In the following example, we’ll open our terminal, make a new directory, set up a bare-bones project and run the scaffolding code.

    $ mkdir CouchbaseExample
    $ cd CouchbaseExample
    $ code example.php

    Create a file with some sample PHP. (You will want to alter the username and password details to match your local database.)

    <?php
    
    use Couchbase\ClusterOptions;
    use Couchbase\Cluster;
    
    $options = new ClusterOptions();
    $options->credentials("Administrator", "password");
    $cluster = new Cluster("couchbase://localhost", $options);
    $bucket = $cluster->bucket("travel-sample");
    
    $options = new ClusterOptions();
    $options->credentials("Administrator", "password");
    
    # authentication with TLS client certificate
    $connectionString = "couchbases://localhost?" .
        "truststorepath=/path/to/ca/certificates.pem&" .
        "certpath=/path/to/client/certificate.pem&" .
        "keypath=/path/to/client/key.pem";
    
    $cluster = new Cluster($connectionString, $options);
    $bucket = $cluster->bucket("travel-sample");
    
    $options = new ClusterOptions();
    $options->credentials("Administrator", "password");
    $cluster = new Cluster("couchbase://localhost?sasl_mech_force=PLAIN", $options);
    $bucket = $cluster->bucket("travel-sample");

    Running Couchbase examples

    Simply use the php command from the terminal:

    $ php example.php

    As you read through the docs, you will see that many code examples link to the PHP SDK docs Github repository. If you wish to run those examples to try things out for yourself, you can clone this repository and run the examples in any directory that contains a .php file, as above.

    Next steps

    That’s it! You are now ready to start developing your Couchbase application.