Export json to file from Neo4j databse using PHP

281 Views Asked by At

I used for that the following command line:

:POST /db/data/transaction/commit {"statements":[{"statement":"match n return n"}]}

when I set this query into a PHP variable, I get the following error:

Fatal error: Uncaught exception 'Neoxygen\NeoClient\Exception\Neo4jException' with message 'Neo4j Exception with code "Neo.ClientError.Statement.InvalidSyntax" and message "Invalid input ':' in C:\wamp\www\PhpProjectNeo4j1\vendor\neoxygen\neoclient\src\Extension\AbstractExtension.php on line 88

Could you please explain to me how can I add this command in PHP?

1

There are 1 best solutions below

2
On BEST ANSWER

Have you looked at the NeoClient documentation ?

The single usage is :

$result = $client->sendCypherQuery('MATCH (n) RETURN n')->getResult();

If you want to export to json, there is no magic in the client, just use the nodes objects returned, create an array and encode it to json :

$nodes = [];
foreach ($result->getNodes() as $node) {
  $nodes[] = [
            'id' => $node->getId(),
            'labels' => $node->getLabels(),
            'properties' => $node->getProperties()
            ];
}

var_dump(json_encode($nodes));

EDIT :

In order to have the result object, you need to have the ResponseFormatting service enabled :

example :

$client = ClientBuilder::create()
    ->addDefaultLocalConnection()
    ->setAutoFormatResponse(true)
    ->build();