PHP Neo4j Library (church/neo4j): Installation, Initialization, and Transaction Operations
This article introduces the PHP Neo4j library church/neo4j, outlines its PHP ≥ 7.4 environment requirements, shows how to install it via Composer, and provides detailed code examples for initializing the client, building statements, managing transactions, and running unit tests.
Introduction
The church/neo4j library is a PHP client for Neo4j that simplifies connecting to the Neo4j HTTP API. It requires PHP ≥ 7.4.
Installation
Install the package with Composer:
composer require church/neo4jUsage – Initialization
$app = new \Church\Neo4j\Application("http://127.0.0.1:7474", "neo4j", "neo4j");
$app->discovery();Usage – Building a Query
$statement = (new \Church\Neo4j\Statement('CREATE (n $props) RETURN n)'))
->params([
'props' => [
'name' => 'test'
]
]);Transaction Management
Add statements to a repository and start a transaction:
$statements = \Church\Neo4j\StatementRepository::add($statement);
$transaction = $app->transaction($statements);
$transaction->begin();Commit the transaction and handle the response:
$result = $transaction->commit();
if ($result->getRawResponse()->getStatusCode() == 200) {
print_r($result->getData());
}Keep the transaction alive (default expiry is 60 seconds):
$transaction->keepAlive();Rollback if needed:
$transaction->rollback();Or begin and commit in a single call:
$result = $transaction->beginAndCommit();
print_r($result);Unit Testing
composer install
./vendor/bin/phpunitFor more details, refer to the Neo4j HTTP API documentation at https://neo4j.com/docs/http-api/current/action/ .
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.