Call osascript through php shell_exec doesn't work - php

I was trying some things with osascript when I had this problem.
Here is my test :
/usr/bin/osascript <<-EOF
tell application "System Events"
activate
display dialog "Hello world"
end tell
EOF
Here is my PHP file.
<?php
$output = shell_exec("./test");
echo "<pre>$output</pre>";
?>
Do you have any ideas why this does not work? (It's not really important but I was just curious about it)

I suspect the osascript executable requires environment variables to be set up to work correctly, which is why this works fine from the console. See this answer to show you how to set these up from PHP.
In my case it was the HOME variable, but it could be something else. Keep adding them until it works!

Related

Using PHP to call Virtualenv’ed Python Script

Last night I spent 5.5 hours trying make PHP execute and receive the output of Virtualenv’ed Python script. Nothing worked; except for scripts that were not Virtualenv’ed.
What I am trying to do:
I am trying to make PHP call a virtualenv’d install of the Newspaper lib output text when I call it.
What I have now:
PHP: (updated)
<?php
$output = exec('newspaper2/bin/python3 /var/www/html/components/python/test.py 2>&1', $output2);
print_r(error_get_last());
echo $output2;
echo $output;
…this works when using a non-virtualenv script
Python: (updated)
from newspaper import Article
url = 'http://example.com/'
article = Article(url)
article.download()
article.html
article.parse()
article.authors
article.publish_date
string = article.text
print(string)
What the issue is:
I can run the script that PHP is running from the command line and it outputs just fine.
What I have tried:
With PHP, (I have tried all the “exec” calls for PHP) it cannot seem to open the virtual environment and returns nothing.
Before the script I have called “python3” and a few other things to no avail.
Yes, I have chmoded it to be executable…
I feel like this should be so simple.
I have tried suggestions on other posts and all over the web to no avail.
Questions:
Did I set up the virtualenv wrong?
At the top of the Python script, instead of the “#!/usr/bin/env python3” should I call something else?
If so, where do I find it? Should I start from scratch and will that
help?
Thank you for your help;
PS: I am running Ubuntu16, PHP7 and I need to use Python3
In the virtualenv'ed scripts (i.e. installed via the setuptools' entry-points), you should not touch the shebang (#!... first line). It is populated by the virtualenv & setuptools & related tools.
If you specify your own shebang, then it is not virtualenv'ed script. In that case, call python directly:
exec('/path/to/venv/bin/python3 /var/www/html/components/python/testing.py');
Alternatively, you can put the absolute path to the virtualenv's python binary to the py-script, but this does not look a good idea.
Also, remember that virtualenvs are non-relocatable. So they should stay in the path where they were created.
Also note that exec() returns only the last line of the output. You probably want shell_exec() or exec('...', $output) to get the whole output.
Also, it is unclear what happens with your script, and what is being printed on stderr. Try this command to see what is the error:
exec('/path/to/script 2>&1', $output)
#OR:
exec('/path/to/venv/bin/python3 /path/to/script 2>&1', $output)
OK, I finally figured it out and learned a lot in the process. The newspaper lib that I am using by default tries to write to the base of the users home directory. In this case, it was attempting to write to www-data, /var/www.
To fix this:
Go to the settings.py file in the newspaper library.
Edit the variable DATA_DIRECTORY = '.newspaper_scraper' and change it to DATA_DIRECTORY = '.path/to/writable/directory'
Save the file and you should be good to go.
I have no idea why it was not returning the errors that would have explained this sooner.
Hope this helps anyone else.
Thank you so much Sergey Vasilyev for your help. I appreciate it greatly.

Issues with exec() on php running a shell script

I'm trying to use a php script for executing a shell script. However I'm having some issues apparently with the web server.
I have a bash script called switch_audio.sh. It basically changes the active audio output of the system.
I also have a script.php that runs the code below:
<?php
echo "It's working";
exec("/var/www/html/switch_audio.sh");
?>
When I execute php script.php it's working fine. However, when I try to run it on the web browser by localhost/script.php I just get as ouput the "echo" part.
I've already tried to:
remove 'exec()' from the disable functions in php.ini;
give permissions for everybody in every folder on this path from "/" to the localhost folder;
Any thoughts about it?
Simple solution: exec() returns a string, but you also have to output it to the user:
<?php
echo "It's working";
echo exec("/var/www/html/switch_audio.sh");
?>
You already said you allowed the shell function on your php.ini configuration but it seems it's not working yet... so maybe:
You didn't restarted webserver service after changes
You have somewhere other statement more restrictive... maybe ini_set in your php files.
As a reminder, be sure that you are doing well on your php.ini file, check it for this kind of statement:
disable_functions=exec,passthru,shell_exec
Maybe you can try instead of exec other similar php function like shell_exec to check if it works. Or maybe is working and not showing anything.

Open executable with php

How to open executable (GNU/Linux) with php?
exec and system are not working for me.
Since I have tried multiple different options, I'm here to ask you. I never had any experience with php (I work with C), so I'm stuck at probably something simple, yet so hard for me. I have tried this:
<?php
string exec('add_user email password');
?>
and many other possibilities.
add_user is an executable written in C++. It is located in root folder, in html folder and apache2 folder (just in case), so it's not the path at fault. Email and password are parameters. I have tried both exec() and system(), nothing happens. I have even tried 'whoami', nothing. When I says nothing happens, I really mean it. I call the php with browser "localhost/test.php", just get blank site. If I try echo 'string'; i still get nothing.
Try shell_exec:
<?php
$output = shell_exec('ls -lart');
echo "<pre>$output</pre>";
?>

PHP (local wamp) - how to print without print dialogue

Trying to find a good way to print without the print dialogue on my LOCAL wamp installation, in other words the printer is connected to the server.
The best (theoretical) way I have found so far seems to be using PHP's exec function, by either running a .bat that will use notepad to open and print the file or by running notepad and printing form there.
EG:
<?php
$exe_tmp = exec('E:\WebServer\www\testprint.bat');
//or
$exe_tmp = exec('c:\WINDOWS\system32\cmd.exe /c "E:\WebServer\www\MOSys\ePos\testprint.bat"');
?>
testprint.bat
NOTEPAD /P E:\WebServer\www\current_reciept.txt
Running either of these form cmd.exe works perfectly but when trying to run it using PHP's exec, when $exe_tmp is echoed, I get seemingly nothing and an output of:
E:\WebServer\www>NOTEPAD /P E:\WebServer\www\current_reciept.txt
If anyone knows why the above don't work when called from exec(); that would be very good, or if anyone knows of another way to bypass the print dialogue that would be excellent.
Cheers
Charlie
I think the answer lies here:http://technet.microsoft.com/en-us/library/cc772773(WS.10).aspx
It would result in something like this:
$exe_tmp = exec('c:\WINDOWS\system32\cmd.exe /c "print /d:\\SERVER\printer e:\WebServer\www\current_reciept.txt"');
I din't test it but according to the microsoft site it sends it directly to the queue

PHP: Messaging/Logging to Eclipse Console?

Is it possible to send messages from a PHP script to the console in Eclipse? Has anyone attempted this already? I'm not very familiar with how the console works, so I'm not sure if there is a standardized method for communicating with it.
If you look at...
Main Menu -> Run -> External Tools -> Open External Tools Dialog.
In there I have set up PHP Codesniffer with the following...
Name : Code Sniffer
Location : /usr/bin/phpcs
Working Directory : ${workspace_loc}
Arguments : --standard=${resource_loc}
That runs the codesniffer as an external tool and all the messages it returns appear in the console. Once you have set it up, click the down arrow and choose "Code Sniffer" and then anything the external program (in this case codesniffer) outputs will be in the Eclipse console.
If you set it up like this...
Name : PHP
Location : /usr/bin/phpcs
Working Directory : ${workspace_loc}
Arguments : ${workspace_loc}${resource_path}
It will just run php in CLI mode and if you run it with Wilco's code (above) you will get.
Hello World
In the terminal.
Hope that helps.
Any echo or print you do should go to the console automatically. This has been very unreliable for a long time, however. Please vote to have this bug fixed at:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=282997
All output from an Eclipse external tool launch goes to the console by default, so if you execute a PHP script using an external tool launcher any output from the script will go to the console.
For example:
<?php
echo "Hello World\n";
?>
Will send "Hello World" to the console.

Categories