PHP code example of graphaware / neo4j-php-client

1. Go to this page and download the library: Download graphaware/neo4j-php-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

graphaware / neo4j-php-client example snippets




raphAware\Neo4j\Client\ClientBuilder;

$client = ClientBuilder::create()
    ->addConnection('default', 'http://neo4j:password@localhost:7474') // Example for HTTP connection configuration (port is optional)
    ->addConnection('bolt', 'bolt://neo4j:password@localhost:7687') // Example for BOLT connection configuration (port is optional)
    ->build();

$client->run('CREATE (n:Person)');

$client->run('CREATE (n:Person) SET n += {infos}', ['infos' => ['name' => 'Ales', 'age' => 34]]);

$result = $client->run('MATCH (n:Person) RETURN n');
// a result always contains a collection (array) of Record objects

// get all records
$records = $result->getRecords();

// get the first or (if expected only one) the only record

$record = $result->getRecord();

$query = 'MATCH (n:Person)-[:FOLLOWS]->(friend) RETURN n.name, collect(friend) as friends';
$result = $client->run($query);

foreach ($result->getRecords() as $record) {
    echo sprintf('Person name is : %s and has %d number of friends', $record->value('name'), count($record->value('friends')));
}

$stack = $client->stack();

$stack->push('CREATE (n:Person {uuid: {uuid} })', ['uuid' => '123-fff']);
$stack->push('MATCH (n:Person {uuid: {uuid1} }), (n2:Person {uuid: {uuid2} }) MERGE (n)-[:FOLLOWS]->(n2)', ['uuid1' => '123-fff', 'uuid2' => '456-ddd']);

$results = $client->runStack($stack);

$stack = $client->stack();

$stack->push('CREATE (n:Person {uuid: {uuid} })', ['uuid' => '123-fff'], 'user_create');
$stack->push('MATCH (n:Person {uuid: {uuid1} }), (n2:Person {uuid: {uuid2} }) MERGE (n)-[r:FOLLOWS]->(n2) RETURN id(r) as relId', ['uuid1' => '123-fff', 'uuid2' => '456-ddd'], 'user_follows');

$results = $client->runStack($stack);

$followResult = $results->get('user_follows');
$followRelationshipId = $followResult->getRecord()->value('relId');


$result->firstRecord(); // Returns the first record of the Statement Result

$result->records(); // Returns all records

$result->summarize(); // Returns the ResultSummary

$summary = $result->summarize();

$query = $summary->statement()->text();

$stats = $summary->updateStatistics();

$nodesUpdated = $stats->nodesUpdated();
$propertiesSet = $stats->propertiesSet();

// Does the statement affected the graph ?
$affected = $stats->containsUpdates();

$query = 'MATCH (n:Address)
RETURN n.address as addr, n, collect(id(n)) as ids
LIMIT 5';
$result = $client->run($query);

foreach ($result->records() as $record) {
  // here we do what we want with one record (one row in the browser result)
  print_r($record);
}

$address = $record->get('addr');

$record->get('addr'); // returns a string
$record->get('n'); // returns a Node object
$record->get('ids'); // returns an array

$addressNode = $record->get('n');
$countries = $addressNode->value('countries');

$record->nodeValue('n');
$record->relationshipValue('r');
$record->pathValue('p');

$tx = $client->transaction();

$result = $tx->run('CREATE (n:Person) SET n.name = {name} RETURN id(n)', ['name' => 'Michal']);

echo $result->getRecord()->value("id(n)");

$stack = $client->stack();
$stack->push('CREATE (n:Person {uuid: {uuid} })', ['uuid' => '123-fff']);
$stack->push('MATCH (n:Person {uuid: {uuid1} }), (n2:Person {uuid: {uuid2} }) MERGE (n)-[:FOLLOWS]->(n2)', ['uuid1' => '123-fff', 'uuid2' => '456-ddd']);

$tx->pushStack($stack);
// or
$results = $tx->runStack($stack);

$stack = $client->stack();
$stack->push('CREATE (n:Person {uuid: {uuid} })', ['uuid' => '123-fff']);
$stack->push('MATCH (n:Person {uuid: {uuid1} }), (n2:Person {uuid: {uuid2} }) MERGE (n)-[:FOLLOWS]->(n2)', ['uuid1' => '123-fff', 'uuid2' => '456-ddd']);

$tx->pushStack($stack);
$tx->pushQuery('MATCH (n) RETURN count(n)');

$results = $tx->commit();

$client = ClientBuilder::create()
    ->addConnection('node1', 'bolt://10.0.0.1')
    ->addConnection('node2', 'bolt://10.0.0.2')
    ->addConnection('node3', 'bolt://10.0.0.3')
    ->setMaster('node1')
    ->build();

$result = $client->run('CREATE (n) RETURN n', null, null, 'node1');

$client->runWrite('CREATE (n:User {login: 123})');

$client = ClientBuilder::create()
    ->addConnection('default', 'bolt://localhost')
    ->registerEventListener(Neo4jClientEvents::NEO4J_PRE_RUN, array($listener, 'onPreRun')
    ->build();

$client = ClientBuilder::create()
    ->addConnection('default', 'http://localhost:7474')
    ->setDefaultTimeout(3)
    ->build();

use Http\Client\Curl\Client;

$options = [
    CURLOPT_CONNECTTIMEOUT => 3, // The number of seconds to wait while trying to connect.
    CURLOPT_SSL_VERIFYPEER => false // Stop cURL from verifying the peer's certificate
];
$httpClient = new Client(null, null, $options);

$config = \GraphAware\Neo4j\Client\HttpDriver\Configuration::create($httpClient);
$client = ClientBuilder::create()
    ->addConnection('default', 'http://neo4j:password@localhost:7474', $config)
    ->build();
bash
composer 


$query = 'MATCH (n:Person) n, n.name as name, n.age as age';
$result = $client->run($query);

foreach ($result->records() as $record) {
    print_r($record->get('n')); // nodes returned are automatically hydrated to Node objects

    echo $record->value('name') . PHP_EOL;
    echo $record->value('age') . PHP_EOL;
}