PHP code example of stefanak-michal / bolt

1. Go to this page and download the library: Download stefanak-michal/bolt 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/ */

    

stefanak-michal / bolt example snippets


// Choose and create connection class and specify target host and port.
$conn = new \Bolt\connection\Socket('127.0.0.1', 7687);
// Create new Bolt instance and provide connection object.
$bolt = new \Bolt\Bolt($conn);
// If needed set requested protocol versions ..you can add up to 4 versions
$bolt->setProtocolVersions(5.4);
// Build and get protocol version instance which creates connection and executes handshake.
$protocol = $bolt->build();

// Initialize communication with database
$response = $protocol->hello()->getResponse();
// verify $response for successful initialization

// Login into database
$response = $protocol->logon(['scheme' => 'basic', 'principal' => 'neo4j', 'credentials' => 'neo4j'])->getResponse();
// verify $response for successful login

// Pipeline two messages. One to execute query with parameters and second to pull records.
$protocol
    ->run('RETURN $a AS num, $b AS str', ['a' => 123, 'b' => 'text'])
    ->pull();
    
// Fetch waiting server responses for pipelined messages.
foreach ($protocol->getResponses() as $response) {
    // $response is instance of \Bolt\protocol\Response.
    // First response is SUCCESS message for RUN message.
    // Second response is RECORD message for PULL message.
    // Third response is SUCCESS message for PULL message.
}

new \Bolt\helpers\Client(
    AProtocol $protocol,
    array $auth = ['scheme' => 'none'],
    ?Closure $logHandler = null,
    ?Closure $errorHandler = null
)

$conn = new \Bolt\connection\Socket('127.0.0.1', 7687);
$bolt = new \Bolt\Bolt($conn);
$protocol = $bolt->build();

$client = new \Bolt\helpers\Client(
    $protocol,
    ['scheme' => 'basic', 'principal' => 'neo4j', 'credentials' => 'neo4j'],
    function (string $query, array $params, array $stats): void {
        // optional: log every executed query
        error_log($query);
    },
    function (Exception $e): void {
        // optional: handle exceptions; if omitted they are thrown normally
        error_log($e->getMessage());
    }
);

// Returns all rows as associative arrays keyed by field name
$rows = $client->query('MATCH (n:Person) RETURN n.name AS name, n.age AS age');
// $rows = [['name' => 'Alice', 'age' => 30], ['name' => 'Bob', 'age' => 25]]

// Returns only the first value from the first row
$name = $client->queryFirstField('MATCH (n:Person) RETURN n.name LIMIT 1');

// Returns the first column value from every row
$names = $client->queryFirstColumn('MATCH (n:Person) RETURN n.name');

// Transaction example
$client->begin();
$client->query('CREATE (n:Person {name: $name})', ['name' => 'Charlie']);
$client->commit(); // or $client->rollback()

// Statistics from the last executed query
$stats = $client->getStatistics();
// e.g. ['nodes-created' => 1, 'rows' => 0]

$conn = new \Bolt\connection\StreamSocket('helloworld.databases.neo4j.io');
// enable SSL
$conn->setSslContextOptions();
$bolt = new \Bolt\Bolt($conn);

$conn = new \Bolt\connection\StreamSocket();
$conn->setSslContextOptions([
    'local_cert'=> 'd:\www\bolt\cert\public_cert.pem',
    'local_pk' => 'd:\www\bolt\cert\private_key.pem',
    'passphrase' => 'password',
    'allow_self_signed' => true,
    'verify_peer' => false,
    'verify_peer_name' => false
]);
$bolt = new \Bolt\Bolt($conn);