4

I would like to execute a program using PHP, a piece of code that will use an RF transmitter to switch in my lamp.

This is achieved from the command line by:

action 63 A on

It is just a C program someone wrote to control the GPIO pins on my raspberry pi. So I make an index.php

<?php
exec('action 63 A off');
?>

It does nothing while:

<?php
echo exec('action');
?>

Gives me the default text output of the program (an explanation on parameters to use). This tells me PHP works, the program is located, it can be executed. But my lamp is not switching on. Moreover, typing on the command line:

php index.php

Does switch my lamp on/off as expected! (Using the first variety of the file) Is Nginx (user http) not allowed to switch the lamp on/off? It is allowed to execute the file, at least, it can make it generate text output.

I also tried:

<?php
system('action 63 A off');
?>

And some more varieties like shell_exec

And thoughts?

Freek
  • 1,097
  • 2
  • 12
  • 30

3 Answers3

2

I solved it. The answer was easy, indeed the user "http" was not allowed to execute things from the wiringpi library which the C-program needed to run.

In the end I simply did:

chmod +s action

(This sets modifies the executable (called "action") to always run with root privileges.) ...and the code ran as expected with the following PHP file (index.php):

<?php
system('action 63 A off');
?>

Thanks for all the help!

Freek
  • 1,097
  • 2
  • 12
  • 30
0

is the C program a binary that you can pass parameters to? If so the following should work.

string exec ( string $command [, array &$output [, int &$return_var ]] )

for example

exec("controller.exe {$data}",$output);

data being parameters to pass into the executable, and output being any response from the executable.

See here for an example

Chris
  • 95
  • 1
  • 1
  • 8
  • I tried `` it did not work. Looking into the permissions issue now... What did work was executing `php index.php` with in there somewhere `` as root. It must be a permissions thing. – Freek Mar 05 '14 at 21:13
  • The echo statement will send a string to output, which in the case of php is generally a browser window. it will not actually execute anything. Try setting the parameters you want to send to the "action" executable to a string variable. `$params="63 A on";` For example. Then use: ` – Chris Mar 05 '14 at 22:50
  • I'm running nginx on Arch Linux ARM (Raspberry Pi) but I think I found the error by `php index.php` as a regular use, it tells me: _wiringPiSetup: Must be root. (Did you forget sudo?)_ So the program I want to run does not give the http user access to WiringPi which I need to sent data to a GPIO pin (which switches my light on via an RF transmitter.) It should be doable by setting wiringpi permissions, I'll look into that later... – Freek Mar 09 '14 at 21:07
0

Considering echo exec('action') is working but not exec('action 63 A off') , it is seeming that the command is failing if there parameters .

If that is the case , why not make two shell scripts / batch files one for On and one for Off , and call those from PHP ?

File switch_on contents :

action 63 A on

File switch_off contents :

action 63 A off

Make sure switch_on and switch_off files are accessible by PHP and have Execute permission .

switch_on.php

<?php
    exec('switch_on');
?>

switch_off.php

<?php
    exec('switch_off');
?>
Uours
  • 2,517
  • 1
  • 16
  • 21
  • I have tried this using shell scripts which I called in this way. It didn't work. Tonight I'll try both your and Chris' solution. Thanks a lot for the swift replies! Btw, is it not strange that: php index.php Does switch my lamp? Anyway, I'll report back in about 12 hours... – Freek Mar 05 '14 at 06:16
  • [Someone already seems to have run into this](https://stackoverflow.com/questions/14667916/php-not-executing-from-webpage-but-is-fine-from-php-cli-on-a-raspberry-pi?rq=1) I see now. Perhaps it _is_ a permissions issue and it works as root but not as user http... As said, I'll report back in about 12 hours. – Freek Mar 05 '14 at 06:30