Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
05-04-2019 04:46 PM
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
Solved! Go to Solution.
05-07-2019 10:16 AM
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
05-04-2019 04:47 PM
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?
05-06-2019 07:58 AM
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
05-07-2019 10:16 AM
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
05-15-2019 08:56 AM
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
05-07-2019 11:08 PM
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
All the sessions of the conference are now available online