PHP code example of dseguy / php-gremlin

1. Go to this page and download the library: Download dseguy/php-gremlin 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/ */

    

dseguy / php-gremlin example snippets


use Gremlin\Driver\GremlinClient;
use Gremlin\Driver\RemoteConnection;
use Gremlin\Process\Traversal\GraphTraversalSource;
use Gremlin\Process\Traversal\P;
use Gremlin\Process\Traversal\__;
use Gremlin\Structure\Order;

$client = new GremlinClient('localhost', 8182);
$g = new GraphTraversalSource(new RemoteConnection($client));

// Plain values back for plain queries.
$names = $g->V()->hasLabel('person')->values('name')->toList();

// Real Vertex/Edge objects for element-shaped results.
$marko = $g->V()->has('name', 'marko')->next(); // Gremlin\Structure\Vertex
echo $marko->value('age');

// The fluent DSL renders straight to Gremlin-lang text - server-side semantics,
// not a local reimplementation of them.
$adults = $g->V()->out('knows')
    ->filter(__::has('age', P::gt(28)))
    ->order()->by('name', Order::Asc)
    ->values('name')
    ->toList();

// addV()/addE() mutate the graph the same way.
$vertex = $g->addV('person')->property('name', 'alice')->next();

final class Person
{
    public int|string $id;
    public string $name;
    public int $age;
}

$people = $g->V()->hasLabel('person')->hydrateAs(Person::class)->toList();
// list<Person>

use Gremlin\Driver\Serializer\GraphSONSerializerV4;

$client = new GremlinClient('localhost', 8182, new GraphSONSerializerV4());

use Gremlin\IO\BulkLoader;
use Gremlin\IO\GraphMLReader; // or GraphSONReader for TinkerPop's line-delimited export format

$reader = new GraphMLReader('/path/to/export.xml');
$loader = new BulkLoader($g, batchSize: 500);

$result = $loader->load($reader, function (int $vertices, int $edges): void {
    echo "loaded {$vertices} vertices, {$edges} edges so far\n";
});

echo "done: {$result->vertexCount} vertices, {$result->edgeCount} edges\n";
bash
composer