Failed to add a node to spatial layer with https post request in Neo4j php

61 views Asked by At

I'm trying to add a node to the index spatial.

 $path = '/db/data/index/node/'.$layer;
    $nodePath = DbConfig::getFullDatabaseServerUrl() . '/db/data/node/' . $nodeId;
    $data = array("key" => "dummy", "value" => "dummy","uri"=>$nodePath);
    $data =json_encode($data);
    DbRestManager::send($path,$data);


public  static function send($path, $data) {
        $url = DbConfig::getFullDatabaseServerUrl() . $path;
        $auth=DbConfig::getUserName().':'.DbConfig::getPassword();
        $authEcoded=base64_encode($auth);

        $headers = array('Accept: application/json; charset=UTF-8','Authorization: Basic '.$authEcoded,'Content-Type: application/json');

        // Open connection
        $ch = curl_init();

        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        // Execute post
        $result = curl_exec($ch);

        // Close connection
        curl_close($ch);

    }

$data={"key":"dummy","value":"dummy","uri":"dev_db.pico-app.com:7473/db/data/node/11"}

Everything seems fine, but the operation doesn't work.For some reason i also can't see error.

1

There are 1 answers

0
Christophe Willemsen On

From the spatial server documentation, adding node to an index has a complete different json payload to send, and to a different endpoint.

You'll have first to create a spatial layer, then create a spatial index and then only add your node to the spatial index.

Example request

POST http://localhost:7575/db/data/ext/SpatialPlugin/graphdb/addNodeToLayer

Accept: application/json; charset=UTF-8

Content-Type: application/json

{
  "layer" : "geom",
  "node" : "http://localhost:7575/db/data/node/54"
}

Instead of using curl in raw, you may want to use neoclient and the spatial extension for it that will remove you all the burden of the http requests and provide useful methdods, a simple example:

<?php

require_once(__DIR__.'/vendor/autoload.php');

use Neoxygen\NeoClient\ClientBuilder;

$client = ClientBuilder::create()
    ->addConnection('default', 'https', 'localhost', 7474, true, 'neo4j', 'password')
    ->setAutoFormatResponse(true)
    ->registerExtension('spatial', 'Neoxygen\\NeoClientExtension\\Spatial\\SpatialExtension')
    ->build();


$client->createSpatialIndex();
$client->createSimplePointLayer();
// Create first some nodes with lat and lon values
// Retrieve them and add them to the spatial index
$q = $client->sendCypherQuery('MATCH (n:Step) RETURN n');
$steps = $q->getResult()->getNodes();
foreach ($steps as $step) {
    $client->addNodeToSpatialIndex($step->getId());
}