Run a script on a computer from php

1.4k Views Asked by At

I'm unsuccessfully trying to execute a shell command from php. The goal is to switch on/off my music player of my computer/server via internet (with my phone for example). Here is what I would be able to do :

I have a very simple file "play.sh" : Code:

xdotool key XF86AudioPlay;
echo "switched";

if I run it ./play.sh, that works (music player switches on/off)

then I have an other very simple php file "play.php" : Code:

<?php echo shell_exec("./play.sh"); ?>

These two files are in the main folder of my server which is a partition of my computer. (I'm using lampp) But music is playing from my computer.

But when I'm going on localhost/play.php, I can see "switched" that showed me the sh file as been executed, but the sound doesn't turn off .

I just tried exec(), shell_exec(), passthru(), system(), .. with ./play.sh and /opt/lampp/.../play.php the result is exactly the same, and there is no error message.. :/

But that's weird, I'm not sure, but I think that what I run on my computer side is not the same than what I run on my server side. I mean that it's like the sound was turning on/off on the virtual server but had no link with the sound of my computer. That could be an explanation.. but then, how to execute a script on my computer from internet/my server..?

Does someone have an idea to do that ? Is it a configuration of apache..?

Thanks !

EDIT : Here is how I solved my problem :

xhost + local:www-data

Thanks for all your replies

2

There are 2 best solutions below

1
On

It may be a permissions issue - keep in mind that when PHP runs a command, it's run as the user the webserver is running as, e.g. www-data or apache or whatever. If your music player is running as your own personal user, your script may not have the ability to change it when run as a different user.

0
On

Shell_exec works. Try to put in your .sh script absolute path to the executables

/usr/bin/xdotool key XF86AudioPlay;