The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Passing 'string' value to Python Scripts
Discuss Passing 'string' value to Python Scripts in the PHP Development forum on Dev Shed. Passing 'string' value to Python Scripts 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:
|
|
|

March 5th, 2013, 05:27 PM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 35
Time spent in forums: 6 h 32 m 59 sec
Reputation Power: 1
|
|
|
Passing 'string' value to Python Scripts
Hi, I'm looking for a way to pass the string value to my Python Script. My Python script looks like this,
Code:
name = raw_input("Enter your name: ")
print name
And my PHP code is like this:
PHP Code:
<?php
echo "HelloPython!!";
$output = null;
exec('python HelloPython.py', $output, $return);
print_r($output);
print_r($return);
?>
<form action="test.php" method="post">
Query Name: <input type ="text" name="Qname">
<input type="submit" value="Confirm">
</form>
What I want to do is I want to enter my name from my PHP page and send that name value to Python script which is 'name'= raw_input("Enter your name here:").
Can somebody please help me find out how can I do this, thank you so much. 
|

March 5th, 2013, 05:50 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
You need to feed something to stdin but exec() won't let you do that. proc_open will.
Untested.
PHP Code:
$input = "HelloPython!!"; // you might need a newline
$desc = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => STDOUT
);
$pipes = array();
$p = proc_open("python HelloPython.py", $desc, $pipes);
if ($p) {
fwrite($pipes[0], $input);
fclose($pipes[0]);
$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
print_r($output);
proc_close($p);
} else {
echo "Could not run script";
}
|

March 5th, 2013, 11:41 PM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 35
Time spent in forums: 6 h 32 m 59 sec
Reputation Power: 1
|
|
Thank you for your reply. I dunno why but it doesn't work somehow. I've seen many tutorials like this too, but i still don't understand quite well. 
|

March 6th, 2013, 12:33 AM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
"Doesn't work" how?
|

March 6th, 2013, 01:07 AM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 35
Time spent in forums: 6 h 32 m 59 sec
Reputation Power: 1
|
|
I'm not sure but I checked $p like this:
PHP Code:
<?php
$input = "My name";
$desc = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => STDOUT
);
$pipes = array();
$p = proc_open("python HelloPython.py", $desc, $pipes);
echo "It's here";
if ($p) {
echo "OK";
} else {
echo "Could not run script";
}
?>
I still got the same result "Could not run script", so maybe something is not right on "$p", but I'm not sure now what is it
|
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
|
|
|
|
|