Bolt icon indicating copy to clipboard operation
Bolt copied to clipboard

PHP library to provide connectivity to graph database over TCP socket with Bolt specification

Logo

Bolt

PHP library for communication with Neo4j graph database over TCP socket with Bolt protocol specification. The Bolt documentation is available at https://www.neo4j.com/. This library is aimed to be low level, support all available versions and keep up with protocol messages architecture and specifications.

DB Tests PHP7 DB Tests PHP8 No DB Tests PHP7 No DB Tests PHP8

Version support

We are trying to keep up and this library supports Neo4j <= 4.4 with Bolt <= 4.4.

https://www.neo4j.com/docs/bolt/current/bolt-compatibility/

Requirements

Keep up with PHP supported versions means we are at PHP >= 7.4.

If you need support for PHP < 7.4 you can use latest v3.x release. Not all new features are implement backwards.

Extensions

  • mbstring
  • sockets (optional) - Required when you use Socket connection class
  • openssl (optional) - Required when you use StreamSocket connection class with enabled SSL
  • phpunit >= 9 (development)

Installation

You can use composer or download this repository from github and manually implement it.

Composer

Run the following command in your project to install the latest applicable version of the package:

composer require stefanak-michal/bolt

Packagist

Manual

  1. Download source code from github
  2. Unpack
  3. Copy content of src directory into your project

Usage

Concept of usage is based on Bolt messages. Available protocol methods depends on Bolt version.

https://www.neo4j.com/docs/bolt/current/bolt/message/

// Create connection class and specify target host and port
$conn = new \Bolt\connection\Socket();
// Create new Bolt instance and provide connection object
$bolt = new \Bolt\Bolt($conn);
// Build and get protocol version instance which creates connection and executes handshake
$protocol = $bolt->build();
// Login to database with credentials
$protocol->hello(\Bolt\helpers\Auth::basic('neo4j', 'neo4j'));
// Execute query with parameters
$stats = $protocol->run('RETURN $a AS num, $b AS str', ['a' => 123, 'b' => 'text']);
// Pull records from last executed query
$rows = $protocol->pull();

Response from database ($rows) always contains n+1 rows because last entry are meta informations.

More info

Transactions

Bolt from version 3 supports transactions and protocol contains these methods:

  • begin
  • commit
  • rollback

run executes query in auto-commit transaction if explicit transaction was not open.

Cypher query parameters

Neo4j PHP
Null null
Boolean boolean
Integer integer
Float float
Bytes Bytes class
String string
List array with consecutive numeric keys from 0
Dictionary object or array which is not considered as list
Structure directory with structures

List or dictionary can be also provided as instance of class implementing Bolt\PackStream\IPackListGenerator or Bolt\PackStream\IPackDictionaryGenerator. This approach helps with memory management while working with big amount of data. To learn more you can check performance test or packer test.

Structures Node, Relationship, UnboundRelationship and Path cannot be used as parameter. They are available only as received data from database.

Neo4j Aura

Connecting to Aura requires encryption which is provided with SSL. To connect to Aura you have to use StreamSocket connection class and enable SSL.

// url without neo4j+s protocol
$conn = new \Bolt\connection\StreamSocket('helloworld.databases.neo4j.io');
// enable SSL
$conn->setSslContextOptions([
    'verify_peer' => true
]);
$bolt = new \Bolt\Bolt($conn);

https://www.php.net/manual/en/context.ssl.php

Another solutions

https://neo4j.com/developer/php/

Support

Buy Me a Coffee at ko-fi.com