The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Run python scripts on PHP
Discuss Run python scripts on PHP in the PHP Development forum on Dev Shed. Run python scripts on PHP PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 25th, 2013, 06:33 PM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 38
Time spent in forums: 7 h 18 m 33 sec
Reputation Power: 1
|
|
|
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:
PHP Code:
<html>
<body>
<form>
Test: <input type="button" value="Submit" onclick="exec('TPHP.py');">
</form>
</body>
</html>
But it doesn't work at all, if someone knows this problem, please help me find out.  Thank you very much.
|

February 25th, 2013, 07:40 PM
|
 |
Lost in code
|
|
|
|
onclick is a JavaScript handler; you cannot run PHP or Python code in it because it executes on the client side.
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:
PHP Code:
exec("/usr/bin/python TPHP.py");
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
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 38
Time spent in forums: 7 h 18 m 33 sec
Reputation Power: 1
|
|
Thank you for your reply, I'm quite new in PHP and JavaScript so I don't know pretty much to do about this stuff. I have followed your instruction and I put exec like you said but looks like it doesn't work anyway, but when i tried to use "onclick" method to load my python's file, it works. Here's the code:
PHP Code:
<body>
<?php echo "TPHP test";
exec("python TPHP.py");
?>
<form>
<input type="button" value="ViewCode" onclick="location="'python TPHP.py'">
</form>
</body>
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 
|

February 26th, 2013, 07:14 PM
|
 |
Lost in code
|
|
|
|
|
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
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 38
Time spent in forums: 7 h 18 m 33 sec
Reputation Power: 1
|
|
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:
PHP Code:
<?php
$output = null;
exec('python HelloPython.py', $output, $return);
print_r($output);
print_r($return)
?>
and the result is Array ( [0] => Hello!Python!Coming! ) 0
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 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|