Export output from py2neo to php script

I have this two scripts:

php

echo shell_exec('python /Users/leonardo/Sites/neo4jTest/server/script_neo4j.py 2>&1')

python

#!/usr/bin/env python
import sys

sys.path.append('/anaconda2/lib/python2.7/site-packages')

from py2neo import Graph

graph = Graph(host="localhost:7687", auth=("neo4j", "Nokia(n95)"))


def importDB():
    cmd = graph.run("MATCH (n:Movie) RETURN n.title").to_table()
    print(cmd)
    return cmd


importDB()

PHP execute the python script, which links to my neo4j database. The query is correctly executed, but i want to export the output of the this script in php. How can I do ?

thanks

What do you mean by "export the output of this script in php"? Do you mean you're trying to determine how to write a file in PHP?

Thanks for the answer.
Practically my JS code do an ajax call to the PHP script. This calls the python script which execute the query. I want to return the output of the query from python to JS code through the php script like an ajax call

There's going to be a performance overhead due to that indirection.

Build up a Python dict (or list of dicts) while you iterate through each result, or use the data() function in py2neo to do it for you.

Use the Python json.dumps() to turn that array into a JSON object and print that out to stdout.

Something like:

...
import sys, json
...
data = graph.run("MATCH (n:Movie) RETURN n.title").data()
print(json.dumps(data))
...

Make sure the JSON is the only thing in the output to your Python code.

shell_exec in PHP will return the output from executed command, which will be JSON. You can directly return this JSON to the client then (if result of a XHR) or convert it into a PHP object with json_decode and manipulate it before returning it.

Hope that helps!

Cheers,
-Ryan

2 Likes

Hi @lx2pwnd

there is no practical way to do that. Neo4j return output in json type data structure. so whatever you are querying will return a result with nodes and relationship in JSON.
so you need to write a parser in python then this data would carry forward to your PHP script.
Directly you can not send data in PHP.

Cheers

1 Like

It would seem to be working. I used .data() like suggested by @ryan.boyd to return a JSON at the ajax call. Anyway, thanks you both for the help.

-Leo

1 Like