10

I am writing a simple application that uses information from a form, passes it through $_POST to a PHP script that executes a python script and outputs the results. The problem I am having is that my python script is not actually running with the arguments being passed in.

process3.php file:

<?php
     $start_word = $_POST['start'];
     $end_word = $_POST['end'];
     echo "Start word: ". $start_word . "<br />";
     echo "End word: ". $end_word . "<br />";
     echo "Results from wordgame.py...";
     echo "</br>";
     $output = passthru('python wordgame2.py $start_word $end_word');
     echo $output;
?>

Output:

Start word: dog
End word: cat
Results from wordgame.py...
Number of arguments: 1 arguments. Argument List: ['wordgame2.py']

At the top of my wordgame2.py, I have the following (for debugging purposes):

#!/usr/bin/env python
import sys
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

Why isn't the number of arguments being passed = 3? (Yes, my form does send the data correctly.)

Any help is greatly appreciated!

Edit: I might add that it does run when I explicitly tell it the start and end word... something like this:

$output = passthru('python wordgame2.py cat dog');
echo $output
3
  • What do you see when you echo 'python wordgame2.py $start_word $end_word' in your php file? Commented Nov 5, 2013 at 4:09
  • After adding that line, it now outputs: Start word: dog End word: cat Results from wordgame.py... python wordgame2.py dog cat Number of arguments: 1 arguments. Argument List: ['wordgame2.py'] Commented Nov 5, 2013 at 4:14
  • 1
    I think the mistake lies in $output = passthru('python wordgame2.py $start_word $end_word'); Try this $output = passthru('python wordgame2.py '.$start_word." ".$end_word); Maybe, python is rejecting the arguments due to $ keyword (or maybe $ is invoking the shell for variable repplacement) Commented Nov 5, 2013 at 4:22

3 Answers 3

19

Update -

Now that I am aware of PHP, the mistake lies in using the single-quotes '. In PHP, single quoted strings are considered literals, PHP does not evaluate the content inside it. However, double quoted " strings are evaluated and would work as you are expecting them to. This is beautifully summarized in this SO answer. In our case,

$output = passthru("python wordgame2.py $start_word $end_word");

would work, but the following won't -

$output = passthru('python wordgame2.py $start_word $end_word');

Original answer -

I think the mistake lies in

$output = passthru("python wordgame2.py $start_word $end_word");

Try this

$output = passthru("python wordgame2.py ".$start_word." ".$end_word);
Sign up to request clarification or add additional context in comments.

3 Comments

Wouldn't the obvious solution be using " instead of '? No need to concatenate when " does variable interpolation.
Hmm...am not much familiar with php (none at all), so only my answer begins with I think...
Also, probably good to use escapeshellarg() on the two arguments passed in when concatenating.
4

Thank you for your contributions. I have figured out my problem with this simple fix:

$command = 'python wordgame2.py ' . $start_word . ' ' . $end_word;
$output = passthru($command);

In order for passthru to properly handle the php variables, it needs to be concatenated into the string before executing.

3 Comments

Instead of posting your answer, which is ashish's answer, please accept his answer.
I had posted this before I saw his answer. Please look at the time stamp.
Actually, I answered 7 seconds before :) The downvote was unnecessary/unverified.
0

well if I understood you want pass a big amount of text like content of something, so the right way is;

$output = passthru("python wordgame2.py ".json_encode($end_word)." ".json_encode($start_word));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.