PHP code example of autoprotect-group / neo4j-php-client
1. Go to this page and download the library: Download autoprotect-group/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/ */
autoprotect-group / 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')));
}
$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
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();
$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;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.