I am trying to call a python script from php. However, I cannot figure out how to return the result to my php script in order to use it there. My php script is as follows:
<?php
$input = 'help';
$output = passthru("/usr/bin/python3.5 /path/test.py '$input'");
echo $output;
?>
whilst my python script 'test.py' is:
import sys
json2 = sys.argv[1]
That's because your python script returns nothing. Just doing:
json2 = sys.argv[1]
Only assigns the value to json2
. I suspect you're trying to do something like:
import sys
json2 = sys.argv[1]
print json2
That seems to work just fine:
~ # php -a
Interactive shell
php > $input = 'help';
php > $output = passthru("/usr/bin/python test.py '$input'");
help
Update:
Based on your comment. You're looking for exec()
instead:
~ # php -a
Interactive shell
php > $input = 'help';
php > $output = exec("/usr/bin/python test.py '$input'");
php > echo $output;
help
Note that your Python script has to output something. Just assigning a variable is not going to work, since PHP cannot interpret your Python code for you. It can only run the script and do something with the script's output.