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

    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    2

    Proc_open problem


    Hi, from my last post, I am trying to pass an input data to my python script, and thanks to user'requinix' that guided me to use proc_open. I'm now having a problem with my proc_open, I've been trying to find the solution but I couldn't.

    Here's my PHP code:

    PHP Code:
    <?php 

    $descriptorspec 
    = array(
       
    => array("pipe""r"),
       
    => array("pipe""w"),
       
    => array("file""error-output.txt""a")
    );

    $process proc_open('python HelloPython.py'$descriptorspec$pipes);
     
    $input 20;
    $exp 2;

    if (
    $process) { 
        echo 
    "Succeed!";
    } else { 
        echo 
    "Failed"
    }  

    ?>
    and my Python script is like this:

    Code:
    input = raw_input('Enter name: ')
    print input
    when I run the script, it echos "Failed" to me, that means something might wrong on '$process'.

    Can someone help me figure out this problem, please
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,381
    Rep Power
    594
    PHP Code:
    if (is_resource($process)) { 
        echo 
    "Succeed!";
    } else { 
        echo 
    "Failed"

    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,990
    Rep Power
    9397
    Do you have error_reporting turned up and display_errors on? Maybe there's an error message to read.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    2
    Thank you for your reply,

    i did try is_resource as well, but the result still "Failed".

    @requinix - No I don't have and error_message to read, how to create and get that?

    Here's the original code I got from you:

    PHP Code:
    <?php 

    $descriptorspec 
    = array(
       
    => array("pipe""r"),
       
    => array("pipe""w"),
       
    => STDOUT
    );

    $process proc_open('python HelloPython.py'$descriptorspec$pipes);
     
    $input 'HelloPython!!';

    if (
    is_resource($process)) { 
        echo 
    "Succeed!";
    } else { 
        echo 
    "Failed"
    }  

    ?>
    the result is still the same, I'm kinda confused now. Many examples about proc_open are similar to this as well.
  8. #5
  9. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,990
    Rep Power
    9397
    Try
    PHP Code:
    <?php  

    error_reporting
    (-1);
    ini_set("display_errors"true);

    $descriptorspec = array( 
       
    => array("pipe""r"), 
       
    => array("pipe""w"), 
       
    => STDOUT 
    ); 

    $process proc_open('python HelloPython.py'$descriptorspec$pipes); 
      
    $input 'HelloPython!!'

    if (
    is_resource($process)) {  
        echo 
    "Succeed!"
    } else {  
        echo 
    "Failed";  
    }   

    ?>
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    2
    Ok thank you, now I can read an error message,

    Notice: Use of undefined constant STDOUT - assumed 'STDOUT' in /home/myne/website/index.php on line 17

    Warning: proc_open() [function.proc-open]: Descriptor item must be either an array or a File-Handle in /home/myne/website/index.php on line 19
    Failed

    I still don't get it, can you explain what happened about this error to me please?
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    2
    Oh I get it now. it is because my STDERR, so I've changed it like this:

    PHP Code:
    $descriptorspec = array(  
       
    => array("pipe""r"),  
       
    => array("pipe""w"),  
       
    => array('pipe''a'
    );  

    $process proc_open('python HelloPython.py'$descriptorspec$pipes);  
       
    $input 'MynE'

    if (is_resource($process)) {   
        
    fwrite($pipes[0], $input); 
        
    fclose($pipes[0]); 
        
    $output stream_get_contents($pipes[1]); 
        
    fclose($pipes[1]); 

        
    print_r($output); 
        
    proc_close($process);   
    } else {   
        echo 
    "Failed";   

    So it works now, but the result i got is "Enter your name:MynE!" which is the first line of my python script:

    Code:
    input = raw_input("Enter your name:")
    print input
    As you can see, the "print input" line doesn't show on my screen. How can I check that the value really gets in? because the result should be like this:

    Enter your name:MynE!
    MynE!

    as followed to my python script.
  14. #8
  15. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,990
    Rep Power
    9397
    That's not actually how stdin and stdout work. When you run the script yourself and type in your name, the terminal you're using will "echo" your input. So you see what you're typing. It will also echo the newline character you type at the end.

    If you want to replicate that behavior you have to do the echoing yourself: read from the process (the name prompt), write the name to it, append that name to the output to simulate the echo, also append the newline, and continue reading from the process. Basically you repeat the "read, append input, read" cycle every time you want to write to the process.

    Separately, by including stderr as a pipe in the descriptorspec you need to read from and close it too. STDERR should work as PHP is supposed to create it for you, but you can use php://stderr if that doesn't work.
    PHP Code:
    $descriptorspec = array(
       
    => array("pipe""r"),
       
    => array("pipe""w"),
       
    // 2 => array("pipe", "w")
       
    => (defined("STDERR") && STDERR STDERR fopen("php://stderr""w"))
    );

    $process proc_open('python HelloPython.py'$descriptorspec$pipes);

    $input 'MynE';

    if (
    is_resource($process)) {
        
    $output stream_get_contents($pipes[1]);
        
    fwrite($pipes[0], $input PHP_EOL); $output .= $input PHP_EOL;
        
    fclose($pipes[0]);
        
    $output .= stream_get_contents($pipes[1]);
        
    fclose($pipes[1]);
        
    // if you use the pipe then uncomment:
        // stream_get_contents($pipes[2]);
        // fclose($pipes[2]);

        
    print_r($output);
        
    proc_close($process);
    } else {
        echo 
    "Failed";

    [edit] Normally you would need to write a newline to the pipe as well, but closing the pipe immediately after may also work for you (depends how the input is read and dealt with in the script). Including that newline is a good idea.
    Last edited by requinix; March 7th, 2013 at 08:21 PM.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    2
    I'm sorry to bother you again, but I quite don't get it yet. How can we be sure that the data is actually getting in. Suppose that i'm writing 2 data into pipes. The result i got is

    "Enter name here:MynE23 Enter age:"

    PHP Code:
    <?php 

    $descriptorspec 
    = array(   
       
    => array("pipe""r"),   
       
    => array("pipe""w"),   
       
    => array('pipe''a')
    );   

    $process proc_open('python HelloPython.py'$descriptorspec$pipes);   

    $input "MynE";
    $input2 "23";
        
    if (
    is_resource($process)) {
        
        
    fwrite($pipes[0], $input);
        
    fwrite($pipes[0], $input2);
        
    fclose($pipes[0]);
      
        echo 
    stream_get_contents($pipes[1]);
        
    fclose($pipes[1]);
        
    fclose($pipes[2]);
        
    proc_close($process);    
    } else {    
        echo 
    "Failed";    
    }
    python code:

    Code:
    input = raw_input("Enter name here:")
    print input
    
    input2 = raw_input("Enter age:")
    print input2
    
    print "Your name is" + input + "and your age is"+ "input2"
    Should I execute(py script after I put the data in pipe? or just proc_open to feed data into stdin is enough?)

    What I expect after running is it should reach to the last line of my python script. I'm sorry for asking you again but I don't get it really.

IMN logo majestic logo threadwatch logo seochat tools logo