Connectors

PHP connector

Install, connect, query and handle errors with the Raft DB PHP connector (PHP 8, core only).

The PHP connector speaks the Raft DB binary protocol (specification) and offers the same operations as every other connector. Download the package from the Download page.

No TLS yet. User names, passwords and tokens travel in clear text. Use only on trusted networks.

Requirements#

PHP 8 or newer, core only (no extensions beyond a default CLI, no Composer packages).

Install#

Download the package from the Download page and unzip it. Either require the autoloader:

PHP
require_once 'raftdb-php-connector/autoload.php';

or use Composer with the unzipped folder as a path repository (composer config repositories.raftdb path ./raftdb-php-connector, then composer require raftdb/php-connector:@dev); the namespace RaftDB\ maps to src/.

Connect#

PHP
// 3rd..5th arguments: user, password, token; 6th: timeout in seconds (default 30.0)
$db = \RaftDB\Client::connect('127.0.0.1', 7480);

Query#

PHP
$db->put('u1', ['name' => 'Asha', 'age' => 31]);      // array or JSON text
print_r($db->get('u1'));                              // array, or null if absent
echo $db->count(['age' => ['$gte' => 30]]);
foreach ($db->scan('SELECT name FROM documents ORDER BY name') as $row) {
    print_r($row);
}
$db->close();

One page of rows (from examples.php)#

PHP
$db->createIndex('age', true);
$page = $db->query(['age' => ['$gte' => 30]], 10);
echo 'page rows: ', count($page['rows']), ' next: ', $page['next'], "\n";

Errors#

RaftDB\RaftDBException extends Exception; getCode() is the server code. Network problems and use of a closed client are plain \RuntimeException. After an AUTH error or any I/O error the client closes itself.

PHP
try {
    $db->count('{"age":');
} catch (\RaftDB\RaftDBException $e) {
    echo $e->getCode(), ' ', $e->getMessage();   // 4 ...
}

Transactions and prepared statements#

The connector has no transaction call and no prepared-statement call: the protocol has none (every operation is one request and one reply, see the specification), and none of the connector's README, tests or examples uses one. Use the MySQL or MariaDB front-end with a standard driver for those: see Standard MySQL and MariaDB drivers.

Notes#

  • Rows are decoded with json_decode(..., true): JSON objects become associative arrays (an empty object becomes []).
  • Floats are sent with their .0 kept, so 1.0 stays a double.
  • A client is not thread-safe and not shareable between processes: one connection per process.

Conformance status#

24/24 conformance scenarios (scenario 23 uses four child PHP processes). The scenarios are listed in the specification page. The package's README.txt has the install steps and a short example.

See also#