Getting Started with PHP

If you haven’t set up a Riak Node and started it, please visit Running A Cluster first.

To try this flavor of Riak, a working installation of PHP is required, and Composer is required to be installed to fetch the client library package.

Client Setup

Download and unzip, or clone the Taste of Riak Sample Code Repository from GitHub (zip, github repository).

From the taste-of-riak directory, use composer to install the Riak PHP 2.0 Client`.

php path/to/your/composer.phar install

# If you did a global install of composer, run this instead:
composer install

If you set up a local Riak cluster using the [[five minute install]] method, change line 11 from ->onPort(8098) to ->onPort(10018).

Next, run php Ch01-CRUD/taste-of-riak.php to run this chapter’s example code. It should output:

Reading Objects From Riak...
Updating Objects In Riak...
Deleting Objects From Riak...
Working With Complex Objects...
Serialized Object:
{"title":"Moby Dick","author":"Herman Melville","body":"Call me Ishmael. Some years ago...","isbn":"1111979723","copiesOwned":3}

Yay, success!

Since we didn’t use PHP’s REPL environment, let’s walk through the code to see what it actually did at each step.

Setting up the PHP Client and connections

include_once 'vendor/autoload.php';

use Basho/Riak;
use Basho/Riak/Node;
use Basho/Riak/Command;

$node = (new Node/Builder)
        ->atHost('127.0.0.1')
        ->onPort(8098)
        ->build();

$riak = new Riak([$node]);

This code will load the library, declare the necessary use statements for our code, and then initialize and configure a Node Builder. Once we call build() on the builder, it will return to us a Node object, which we use when building our Riak commands.

We are now ready to start interacting with Riak.

Next Steps

CRUD Operations