Using Neo4j from PHP

Goals
If you are a PHP developer, this guide provides an overview of options for connecting to Neo4j. While this guide is not comprehensive it will introduce the different drivers and link to the relevant resources.
Prerequisites
You should be familiar with graph database concepts and the property graph model. You should have created an Neo4j AuraDB cloud instance, or installed Neo4j locally When developing with Neo4j, please use Java 8 or 11 and your favorite IDE.

Intermediate

Neo4j for PHP Developers

php logo

Alternatively, Neo4j can be installed on any system and then accessed via its bolt and HTTP APIs. We recommend the PHP Client for easiest development over bolt and HTTP APIs. You can also directly access the bolt protocol via the PHP Bolt library.

Neo4j Community Drivers

Members of the each programming language community have invested a lot of time and love to develop each one of the community drivers for Neo4j, so if you use any one of them, please provide feedback to the authors.

The community drivers have been graciously contributed by the Neo4j community. Many of them are fully featured and well-maintained, but some may not be. Neo4j does not take any responsibility for their usability.

Full client

Neo4j-php-client is a client supporting multiple protocols. Http and bolt are supported, starting from neo4j 3.5 up until the most recent version.

It boasts many features such as multiple connections, transactions functions, authentication and auto-routing. It also integrates with the psalm static analysis tool for complete type safety.

It is being actively developed and has a big readme file on the Github page.

Author

Ghlen Nagels

Source

https://github.com/neo4j-php/neo4j-php-client

Package

https://packagist.org/packages/laudis/neo4j-php-client

Php

7.4 / 8.0+

Neo4j

3.5 / 4.0+

Protocols

Bolt, HTTP

Example App

https://github.com/neo4j-examples/movies-neo4j-php-client

Installation
composer require laudis/neo4j-php-client
Example
$client = Laudis\Neo4j\ClientBuilder::create()->withDriver('default', 'bolt://neo4j:password@localhost')->build();

$result = $client->run(<<<'CYPHER'
MERGE (neo4j:Database {name: $dbName}) - [:HasRating] - (rating:Rating {value: 10})
RETURN neo4j, rating
CYPHER, ['dbName' => 'neo4j'])->first();

$neo4j = $result->get('neo4j');
$rating = $result->get('rating');

// Outputs "neo4j is 10 out of 10"
echo $neo4j->getProperty('name').' is '.$rating->getProperty('value') . ' out of 10!';

Bolt

A low level driver for the Bolt protocol in PHP.

Author

Michal Stefanak

Source

https://github.com/neo4j-php/Bolt

Php

7.1+ / 8.0+

Neo4j

3.0+ / 4.0+

Protocols

Bolt

Installation
composer require stefanak-michal/bolt
Example
$bolt = new \Bolt\Bolt( new \Bolt\connection\StreamSocket() );
$protocol = $bolt->setProtocolVersions(4.1)->build();
$protocol->hello( \Bolt\helpers\Auth::basic('neo4j', 'neo4j') );

$protocol->run(<<<'CYPHER'
MERGE (neo4j:Database {name: $dbName})-[:HasRating]-(rating:Rating {value: 10})
RETURN neo4j, rating
CYPHER, ['dbName' => 'neo4j']);

$result = $protocol->pull()[0];

$neo4j = $result[0];
$rating = $result[1];

// Outputs "neo4j is 10 out of 10"
echo $neo4j->properties()['name'] . ' is ' . $rating->properties()['value'] . ' out of 10!';