#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    42
    Rep Power
    2

    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.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,143
    Rep Power
    9398
    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(
        
    => array("pipe""r"),
        
    => array("pipe""w"),
        
    => 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";

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    42
    Rep Power
    2
    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.
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,143
    Rep Power
    9398
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    42
    Rep Power
    2
    I'm not sure but I checked $p like this:

    PHP Code:
    <?php 

    $input 
    "My name"

    $desc = array( 
        
    => array("pipe""r"), 
        
    => array("pipe""w"), 
        
    => 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

IMN logo majestic logo threadwatch logo seochat tools logo