Neo4j REST API and PHP

I was trying out Neo4j due to my curiosity of the graph specialization. Although Neo4j is not designed to run stand-alone like a database server there is a sub-project that adds a REST API to allow non-Java applications to make use of Neo4j. Neo4j is pretty simple, you basically have nodes, relationships, and properties on both. That’s about it.

In this post is a very basic hypothetical scenario (mainly about Neo4j) of a digital posting system where:

  • Users can have a relationship with other users
  • Users can post messages
  • Users can hide other users’s messages

Please be aware that I have not thoroughly tested this.

Just so you have a visual idea of what it looks like inside Neo4j I wrote a simple graph via Graphviz. Green = KNOWS, Blue = POSTED, and Red = HIDES.

Here’s some snippets of code (that should really be cleaned up to more reflect the image but I know you will understand) to create some nodes and relationships using the Neo4J-REST-PHP-API-client library. This is minimal to maintain clarity so feel free to add more users, messages, and relationships.

//client setup
$graphDb = new GraphDatabaseService('http://localhost:9999/');
$uri = 'http://localhost:9999/node/1/traverse/node';

$f1 = $graphDb->createNode();
$f1->user_id = 1;

$f2 = $graphDb->createNode();
$f2->user_id = 2;

$f2m1 = $graphDb->createNode();
$f2m1->message = 'f2 post';
$f2m1->timestamp = time();

//User_A knows User_B
$f1rf2 = $f1->createRelationshipTo($f2, 'KNOWS');
//User_B posting Post_B_1
$f1rm2 = $f1->createRelationshipTo($f1m2, 'POSTED');

//User_A hiding User_B's Post_B_1
$f1rf2m1 = $f1->createRelationshipTo($f2m1, 'HIDE');

And finally, the traversal:

$traverse = array(
	"order" => "breadth first",
	"prune evaluator" => array(
    	"language" => "builtin",
    	"name" => "none"
	"return filter" => array(
    	"language" => "javascript",
    	"body" => "(function shouldReturn(pos) {
    		var node = pos.node();
    		if(!node.hasProperty('message')) {
    			return false;
    		var rels = node.getRelationships();
    		var relIterator = rels.iterator();
    		var rel = null;
    		while(relIterator.hasNext()) {
    			rel =;
    			if( (rel.getType() == 'HIDE' || rel.getType() == 'POSTED')
    				&& rel.getStartNode().getId() == 1) {
    				return false;
    		return true;
   	//"max depth" => "1"
var_dump(HTTPUtil::jsonPostRequest($uri, $traverse));

The Neo4j REST component embeds the Rhino engine so the traversal is actually written in javascript. The “return filter” function is only passed a position object of the traversal’s current position. Other objects can be retrieved such as the current node or last traversed relationship via this variable. Only the nodes that return true in the “return filter” are returned from the traversal.

This traversal can be paraphrased as “find UserA’s (identified by an ID of 1) friend’s message postings that have not been hidden by UserA”. We’re only interested in messages so skip (by returning false) any nodes without a “message” property. We then get all the relationships on that node, grab an iterator to loop over the relationships, and skip it if that node has been posted or hidden by UserA.

This entry was posted in Neo4j, PHP and tagged , , . Bookmark the permalink.

5 Responses to Neo4j REST API and PHP

  1. Pingback: Tweets that mention Neo4j REST API and PHP | Rob Olmos --

  2. conandor says:

    Can you explain more on the traverse. Not much info from the documentation.

  3. Ashish says:

    Hey Rob,

    I am a beginner with Graph Databases. To integrate neo4j with PHP, i need to use PHP API. But I am not able to find some good references to start with. Can you suggest some solution to this. Other than neo4j are there any other Graph Databases which bind well with php and have enough references available?

  4. Ashish Trivedi says:

    Hello Rob,

    It would be great if you can post some articles for beginners to neo4j, including installation and step-by-step guide.


Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>