Run Python script from PHP - php

I want to run Python script from PHP. The script's syntax is good, when I run it from command line it generates an image file next to it. When I run the PHP file (contains Python exec) from command line it generates an image file. It's good. The problem is: when I use browser and run PHP it isn't generate any image file. I use Xubuntu OS.

Does the script that you are trying to run requires sudo privileges? If so, you will need to add the user "www-data" or web user with the command (full path) to the sudoers file (Ubuntu /etc/sudoers or execute visudo command).
More info here https://help.ubuntu.com/community/Sudoers
Another thing to look at is at memory_limit in your php.ini directive. You might be using all memory allowed to use.

The problem was the matplotlib backend setting. I changed to 'Agg' and the plot function worked fine.

Related

Windows task scheduler last run result 0xff

I want to run php script through windows task scheduler.
Following is the content of .bat file -
cd C:\test_folder
"C:\Program Files (x86)\PHP\v5.6\php.exe" -f test.php
This .bat file executes successfully if I run it manually but fails in task scheduler showing last run result as 0xFF.
I have multiple versions of PHP installed and have to run test.php on older version. That is why I have to mention full path of php.exe even though PHP environment variable is set.
Do any of the folders have restricted access? Try setting it up so that the scripts runs with highest privileges or that the account it's running under has proper access to the folders required and see if that fixes it.
Also, try dropping the "-f".

Double click and run the Php script on mac

I am sorry if this question was answered.
Why can't I run php code directly without using terminal on mac.What I mean when you double click on html file it automatically opens in the browser but not in the case of php.If I try to double click on php it opens with some text-editor.
Any help would be helpful.
Try this (for mac),
Open terminal
cd to folder
Start php server - php -S 127.0.0.1:8000
Open browser and enter - http://localhost:8000/file-name.php
I think you don't understand what PHP is ...
HTML is a markup-language, that can directly be understood by the browser. If the browser opens the file, it can do something with the content.
As PHP is a programming-language, you need a parser. This parser is your PHP executable. This program can understand PHP and does nothing more, than running the code and giving something as result. This result may be an HTML webpage, an image or whatever.
Since you said, you're using a mac, here's a quick introduction on how to set up your personal webserver:
On Mac OSX, PHP and Apache (that's what I use in this example) is already installed and pre-configured. You can just start using it like this:
Go into your system preferences and verify that Web Sharing is enabled.
Open the Finder and go to /Library/WebServer/Documents/localhost. All files that are in there are processed by the local webserver (Apache and PHP, if you want to know that). Place your file in there and open your webserver and call http://localhost/YourFile.php and it will call the file YourFile.php and show you what the output of the script is.
EDIT:
If you are using PHP for scripts, like bash-scripts, see the answer #andreas-baumgart provided.
To run PHP in MAC, one should start the built-in Apache Web server and also enable the PHP already installed.
This can be done with the following steps.
Go to /etc/apache2/httpd.conf and change the permission to sudo chmod 777 httpd.conf
Then open the above file to uncomment the line #LoadModule php5_module libexec/apache2/libphp5.so
To start the apache built-in server, use the command sudo apachectl start in the terminal.
Now .php files can be created and run from the terminal using php -f filename.php and it can also be run on a browser using http://localhost/filename.php
You cannot execute plain PHP scripts as they are no executable programs but source code. As such they contain just the receipt for an interpreter to create executable code. In order to run your PHP script you need to pass it to the PHP interpreter. In your scenario you can archive that by providing a shebang.
To run your script on double click try this:
Make the script executable using chmod +x yourscript.php
Prepending the according Shebang to the files content: #!/usr/bin/env php.
Select a PHP file in Finder, hit CMD-i and change "Open With" to "Terminal.app".
Late response, but was looking into doing this for myself, this coming up as one of the results in my searching wanted to provide 2 solutions since I ultimately came to both on my own.
Solution #1
The simple way is to go a round about way by writing a wrapper file to execute the script you're working on. Create a file with the following code:
#!/usr/bin/php
<?php
include('name-of-php-script.php');
?>
Save it as wrapper.command The name wrapper isn't important, but the command extension tells Finder that this is a shell script to open up in Terminal. The file itself just executes whatever php script is in the include.
Solution #2
The specific inquiry requires a bit of work.
First make sure that the 1st line of the php script is:
#!/usr/bin/php
This is where the preinstalled version of PHP is installed on Mac OS X. You can always verify by running this command in terminal:
whereis php
Once you've added the Shebang line to the php script you've readied it for automatic execution.
To make it double clickable executeable you have to do the following:
Right click on the PHP script and click Get Info. Click where it says Open With, click the default option to see all the available options. Select Other...
Switch where it says Enable: from Recommended Applications to All Applications, and click the checkbox for Always Open With. Choose Terminal as the application. Finally, you have to click the button that says Change All...
OS X will verify you want it to set Terminal as the default application to open .php files
This will make every php file open up in terminal by default, but unless they contain the #!/usr/bin/php line they won't actually run.
Try MAMP
MAMP 4 brings even more opportunities for web developers. We are now supporting MySQL 5.6 and Nginx is now fully integrated. Server starting times have been improved.
Because .php files are not 'executable' per se, instead they are just text files with a PHP extension.
You need to run the php interpreter against the file to execute on it's contents.

Can I execute the "cd" command to change directories using PHP CLI?

I created a script using PHP CLI that I would like to have cd me into a directory based upon my command line input.
While I can get the PHP execution commands to echo output (e.g. echo ls -al), I cannot get them to run cd.
I have searched a lot online to find the solution, but have come up empty.
You can't use cd as it would run in a subshell, and the changed working directory would be lost when you returned to PHP before issuing the next command.
Use chdir instead.
You need to run chdir from php, running cd from exec, system, shell_exec etc. only change directories in subprocesses called by php, every new system call will start in php current working directory.

Using PHP to execute terminal commands

How can PHP be used to create a number of commands in the terminal? For example, if I want to change the permissions on a folder or a file, I might run
Sudo Chmod 777 FileName
from the command line. How could I do this with PHP?
Look at exec, system etc: http://www.php.net/manual/en/book.exec.php
Obviously, if you are going to use sudo for root access stuff (not recommended) then you would need to either supply a password somehow or set the user which PHP is running as to not require a password.
It may be better to use an SSH call for this and supply the command to execute via that command.
To sum up, I really don't recommend using PHP (or any language for that matter) to run a series of commands which then uses sudo
To give permission to folder or file in php use following line of code
chmod($file,0777);
this will change the permission of file or folder.

How do I run a PHP script using windows schedule task?

I have installed localhost/server in my machine and I need to run a php script using windows schedule task. how do I add path in "Actions" tab in schedule task / cofigure the script to run for particular period?
Locate the php.exe executable on your system and pass it the name of the script file using the -f parameter.
Example:
C:\Xampp\php\php.exe -f C:\Xampp\htdocs\my_script.php
Reference:
Introduction to using PHP on the command line
PHP command line options
Here's how did it.
Windows scheduler -> create a new task -> action tab -> Edit
At least I tried out some suggestions but it doesn't work so I tried this.
Use a bat file and schedule to execute that bat file.
For example in the bat file executephp.bat, write this
c:\xampp\php\php.exe -f c:\xampp\htdocs\do_something.php
save that bat file that contains that line.
Go to windows scheduler and create a new task and in action tab, browse to point that executephp.bat and for start in -> direct to the directory u have that executephp.bat.
For example if u save the file under C:\xampp\htdocs put that C:\xampp\htdocs in the start in.
Remember to invoke the script even when the user is not logged on.
Everything is set and it will execute without problem.
You can use PHP Command Line to execute it rather then trying to load it through the browser.
Under the actions tab, create a new action and:
Program/Script: Point to your PHP.exe file
Add Arguments: -f /path/to/php/file.php
Optionally you can make it start in the script's directory as well.
create Schedule task
Scheduler->Actions->Edit Action
if you have php file
Program/script: powershell
Add arguments: curl http://localhost/demo/cron.php
if you have MVC/CMS URL
Program/script: powershell
Add arguments: curl http://localhost/demo/controller/method
I just wanted to leave what i had to do to get this working for server 2012. Which was what has previously been said but with added quotes and using the 'Add arguments' box. So in Task Scheduler->Actions->Edit Action.
Program/script: "C:\xampp\php\php.exe"
Add arguments: -f "<full path and filename>"
Start in: <Path to file>
(I had to use 'start in' as i referred to class's within the script)
Hope this helps.
Here's how I did it.
In the Run box: c:\location_of_my_php_installation\php.exe -f c:\location_of_my_php_file\php_file.php
In the Start in box: c:\location_of_my_php_installation\php.exe
you can directly call your local host url by using
explorer "http://localhost/yourFile.php"
If the answer given by Pekka does not work (C:\Xampp\php\php.exe -f C:\Xampp\htdocs\my_script.php), make sure that you have the correct PHP extensions enabled as well as the correct php.ini file being used for the PHP version you are using.
I ran into this issue recently and resolved it. I was using PHP v5.4 to run my script.php that was nested within the top level PHP folder (v5.3). When I ran the script.php, from within the v5.4 folder, it was using the v5.3 php.ini file with different extensions, which caused the script.php to fail.
To fix this, here is what I did within the Task Scheduler : Actions tab
Program Script:
"C:\Program Files (x86)\PHP\v5.4\php.exe"
Add Arguements:
-c "C:\Program Files (x86)\PHP\v5.4\php.ini" -f "C:\Xampp\htdocs\script.php"
Using the -c option, you can specify which php.ini file should be used (stackflow answer).
I verified which php was in use by following this stackflow answer.
In command line, type php -m to check which extensions are enabled.
Then, use php --ini to check which .ini file(s) is/are being read by PHP.
You may not be using the correct ini file for the PHP version you are using if you have multiple versions installed.
You dont need a .bat file just set the options in the task scheduler:
So here i've just pointed to the php.exe and then in the arguments the
-f fullpath2PHP file
The important bit is the Start in part - this should be the folder your php file (the one you want to run) sits in.
so for example:
File location
-f c:\inetpub\wwwroot\runthisplease\myFile.php
Start in
c:\inetpub\wwwroot\runthisplease\
You can test this by creating a windows shortcut (but you cannot schedule a shortcut link)
In the properties for your task in the Task Scheduler, change the account for which your script is running under to the SYSTEM account.
Beaware! You should ONLY do this for scripts you fully trust - Doing this elevates the privileges to of the script to beyond administrator.
To create a scheduler in Windows, you need to:
Create .bat file on your server or system;
Type the following command in your.bat file: “F:\xampp\php\php.exe” -f “F:/xampp/htdocs/sitefolder/test.php”;
Set the scheduler time and file in your task scheduler in Windows.

Categories