【问题标题】:Export json to file from Neo4j databse using PHP使用 PHP 将 json 从 Neo4j 数据库导出到文件
【发布时间】:2015-08-13 20:09:55
【问题描述】:

我使用了以下命令行:

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

当我将此查询设置为 PHP 变量时,我收到以下错误:

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

能否请您解释一下如何在 PHP 中添加此命令?

【问题讨论】:

  • 你的代码现在是什么样子的?给你这个异常的 PHP 代码是什么?
  • 另外,您是否按照异常提示查看了 AbstractExtension.php 的第 88 行?
  • 我现在的代码是:$queryx = ":POST /db/data/transaction/commit {\"statements\":[{\"statement\":\"match n return n\"}]}"; $resultx = $client->sendCypherQuery($queryx)->getResult();
  • 在第 88 行,我有以下代码:` public function checkResponseErrors($response) { if (isset($response['errors']) && !empty($response['errors']) ) { throw new Neo4jException(sprintf('Neo4j Exception with code "%s" and message "%s"', $response['errors'][0]['code'], $response['errors'][0 ]['信息'])); } } `
  • @jjaderbeg 如果您知道任何可以使用 PHP 从 Neo4j 数据库中导出 Json 文件的方法,请告诉我

标签: php json neo4j neoxygen


【解决方案1】:

您看过 NeoClient 文档吗?

单一用法是:

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

如果要导出为json,在客户端没有什么神奇的,只需使用返回的nodes对象,创建一个数组并编码为json:

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

var_dump(json_encode($nodes));

编辑:

为了获得结果对象,您需要启用 ResponseFormatting 服务:

例子:

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

【讨论】:

  • 感谢您的反馈。我收到以下错误:致命错误:调用 C:\wamp\www\PhpProjectNeo4j1\index.php 中非对象上的成员函数 getNodes()
  • 你能发布你的代码用于 1. 构建客户端 2. 发送查询并输出 json。
猜你喜欢
  • 2019-12-29
  • 1970-01-01
  • 1970-01-01
  • 2015-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
相关资源
最近更新 更多