February 25th, 2013, 06:33 PM
Run python scripts on PHP
Hi, I'm new in PHP and 'm looking for the way to run my Python scripts via PHP. I'm now using WINSCP to edit my index page where I can store my python code here. I have found some simple code to execute like this:
But it doesn't work at all, if someone knows this problem, please help me find out. Thank you very much.
Test: <input type="button" value="Submit" onclick="exec('TPHP.py');">
February 25th, 2013, 07:40 PM
In a PHP script, you can run a python script using the exec function:
Depending on your server configuration, invoking the python script using the python binary may or may not be required. You might also be required to specify the full path to the python binary, like:
If in doubt, ask your web host or server administrator.
As far as building a form to do it, you need two separate pages. For for the form, and one to process the form. The action attribute of the form tag specifies the PHP file that will be requested when the form is submitted.
February 26th, 2013, 12:55 PM
So my question is if onclick "location" can find my script by just typing the name of my python script, so the location for exec() should be the same right? Please help
<?php echo "TPHP test";
<input type="button" value="ViewCode" onclick="location="'python TPHP.py'">
February 26th, 2013, 07:14 PM
Your form or button or link needs to take the user to a different PHP page. On the second PHP page, you need to run the exec call.
You cannot call the Python file from the form unless your web server is configured to serve Python scripts and your form takes the user to the URL of that Python script directly. However, in that case, you would not be calling Python from PHP.
February 27th, 2013, 11:22 PM
Hi, sorry to bother you again, I'm really curious about this so i tried to run the python script that can just print "Hello Python" on the screen, and it works, here's the code:
and the result is Array (  => Hello!Python!Coming! ) 0
$output = null;
exec('python HelloPython.py', $output, $return);
But when I change my python script to "TPHP.py" (This script is about to insert text to database just one line) it said:
Array ( ) 1 , it returns value 1 and the array is empty, moreover the data doesn't get in the database. Do you know why? Please help