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.
Related
this is my first time coding on python, i have a nested array that i want to parse it to python where the json will be process there and return the data back to php. But since this is my first time coding on python i have no idea how to parse json data from php to python vice versa. all i know is how to run python script from php using exec().
PHP
$alternative_val = array(
array('1','0.5','3'),
array('2','1','4'),
array('0.333','0.25','1')
);
$json_alter = json_encode($alternative_val);
$output = shell_exec('python test.py');
echo $output;
after python receive the json data from php i should go into a list,
expected python result
X = [['1','0.5','3'],['2','1','4'],['0.333','0,25','1']]
print(X)
You can use json_encode function for your php script and modify your shell command with arguments. And after that you will get your json data in py script.
PHP:
$alternative_val = array(
array('1','0.5','3'),
array('2','1','4'),
array('0.333','0.25','1')
);
$json_alter = json_encode($alternative_val);
$output = shell_exec('python test.py "' . $json_alter . '"');
echo $output;
PYTHON:
import sys
x = sys.argv[1]
print(x)
I am trying to run a Python script inside PHP and show the output.
I tried with simple test.py inside PHP, which print hello world without problem. But when I try to execute my desired command from PHP script the exec() returns empty string instead of output.
So the here is my php script. Below snippets works fine:
$output = exec("python test.py");
var_dump($output);
But not the desired one.
$command = "python -m scripts.label_image --graph=tf_files/retrained_graph.pb --image=".$uploadfile;
$output = exec($command);
var_dump($output);
so the folder structure is:
/var/www/mysite/
-scripts(folder which contains python scripts)
-tf_files(folder with other files)
-uploads (image folder)
Can someone tell me what is going wrong?
Sample output in stdout/shell:
the variable $output should an argument.
If you do this, you should get only the last output of your script.
exec("python test.py", $output);
var_dump($output);
https://www.php.net/manual/en/function.exec.php
Here is my code, that is supposed to work according to answers to other similar questions, but it does not.
PHP:
<?PHP
$par = $_POST["parameter"];
$importPar=exec("py pyData.py . $par"); //also tried shell_exec()
print ($par);
print($importPar);
?>
Python:
import sys
who = sys.argv[1]
print("This is php var: ",who)
It might have to do something with my cmd because nothing is returned even when I try:
$test=shell_exec('ipconfig');
echo $test;
With minor modifications, your code works just fine on my PHP 5.6.3 setup. The only substantive difference in my example below is that $par gets set locally, and not from a $POST input.
Minor corrections:
1. The . isn't necessary when you're including a $variable inside double quotes. This will actually make python think you want to print . instead of $par, as . is read as sys.argv[1].
2. I'm assuming py is an alias, but I needed to use python in exec() to run properly.
3. You'll get a tuple as print output with your python statement as-is: ('This is php var: ', 'foo')
. Consider using .format() instead, see below.
""" test.py
import sys
who = sys.argv[1]
print('This is php var: {}'.format(who))
"""
<?PHP
$par = "foo";
$importPar=exec("python test.py $par");
print ("$par\n");
print($importPar);
?>
Output:
foo
This is php var: test.py
Process finished with exit code 0
I Have been stuck for a couple of days now. I am attempting to call a simple python script from PHP. For the life of me I cannot figure out what the issue is. I have made the onoff.py script executable with chmod +x.
I can run the script just fine from the command line like this:
pi#raspberrypi:/var/www/html $ python onoff.py
LED on
LED off
My issue is when I try to call the script from PHP. I get nothing.
My Python Script:
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
print "LED on"
GPIO.output(18,GPIO.HIGH)
time.sleep(1)
print "LED off"
GPIO.output(18,GPIO.LOW)
My PHP Script:
<?php
$command = escapeshellcmd('python /var/www/html/onoff.py');
$output = shell_exec($command);
echo $output;
?>
Any help is greatly appreciated!
EDIT:
if I change my onoff.py script to a simple while loop such as:
#!/usr/bin/python
x=1
while (x<10):
print x
x=x+1
the output on the browser is:
1 2 3 4 5 6 7 8 9
I just don't understand why the loop will run but I get no output with the original python code.
EDIT 2:
Ok So I taking a different approach and trying to see where the code fails. I am adding bits of code at a time. Please see the following.
#!/usr/bin/python
import random
import time
import RPi.GPIO as GPIO
randomNumber = random.randint(1, 20)
GPIO.setmode(GPIO.BCM)
#GPIO.setup(18,GPIO.OUT)
print randomNumber
Now when I run the PHP it shows a random number so I know the python script is running. When I un-comment GPIO.setup(18,GPIO.OUT) and run the php I get a blank screen. I have no idea why this would make the script fail.
shell_exec() will only return a string if $command a). Ran OK and assuming b). that it spits its response to STDOUT.
Use exec() and pass it your command, an integer $code and an empty array $response both of which are treated by PHP as arguments by reference.
Run your command thus:
$command = escapeshellcmd('/path/to/python /var/www/html/onoff.py');
$response = array();
$code = 0;
exec($command, $response, $code);
var_dump($code, $response);
die;
You should now see what is actually being given to PHP internally and why the Python script isn't working.
You need to use python before the script, i.e.:
$command = escapeshellcmd('python /var/www/html/onoff.py');
If it doesn't work, python probably ins't on the PATH of the apache user and you may need to use the full path to the python binary, use which to find the full path:
which python
//usr/bin/python
The use that value:
$command = escapeshellcmd('/usr/bin/python /var/www/html/onoff.py');
Note:
Make sure apache user has execute permissions on onoff.py
Php never get response If I use sleep for 5 minutes or more in python. Using sleep around 2 minutes , its working, I do not know what is happening, where is the problem?
Sample Code
uploadfile_database.php:
<?php
$file_id=1;
$response=exec('python /home/xyz/test.py '.$file_id);
echo $response;
?>
test.py
import os, json
import sys
sleep(300)
#sleep(420)
print "hello"
exec does not work as you're expecting. To get the output from of your python script you need to pass an output parameter to exec.
Ex.
$output = array();
$exit_code = exec('python /home/xyz/test.py '.$file_id, &$output);
You could also just use shell_exec which would work as you're expecting