1

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.

1

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok so I made the changes the output when I load the php page is: int(1) array(0) { }
Hmmm sorry to say "it works for me" (Note I stripped it right down and removed escapeshellarg() as that's on;y really used if you have user input munged into a CLI command) #> php -a $cmd = '/usr/bin/python /path/to/test.py'; $code = 0; $res = []; exec($cmd, $res, $code); var_dump($code, $res); die;
0

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

8 Comments

pi@raspberrypi:~ $ which python /usr/bin/python
What are the permissions of onoff.py ?
I replaced my original code with this: $command = escapeshellcmd('/usr/bin/python /var/www/html/onoff.py'); still no go:(
Permissions: -rwxr-xr-x 1 root root 221 May 15 23:35 onoff.py
What does output? $command = escapeshellcmd('python /var/www/html/onoff.py'); echo $command;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.