2

I have some variables defined in a PHP file. I then call a python script from the PHP with these variables as arguments. HOWEVER the values of the variables do not carry over to my python script, it seems as though they as passed as strings and not as variables:

$first = "doggy";
$second = "kitty";

$command = escapeshellcmd('python ./script.py $first $second');
$output = shell_exec($command);

The above code produces not "doggy" and "kitty" respectively in my Python script, but literally "$first" and "$second". I want doggy and kitty.

For example when I print in Python:

print sys.argv[1];
>>$first

is the output I am receiving.

My Python script is NOT outputting anything, the script interacts with an API that I wish to use these variables with.

I have tried these previous posts which seem to be near what I am asking. I am either not understanding them, or they are not working. Some answers are too technical or too vague.

Passing value from PHP script to Python script

PHP <-> Python variable exchange

If shell_exec is not the best way for this, I am open to new ideas. Any and all help is appreciated. Thanks for taking the time to look at my post.

LC-Data
  • 80
  • 9

1 Answers1

3

A single quoted string will be displayed literally, while a double quoted string will interpret things like variables and new line sequences (\n).

Therefore, change from single to double quotes:

$command = escapeshellcmd("python ./script.py $first $second");

Read more about strings in the PHP manual: http://se1.php.net/manual/en/language.types.string.php

Emil
  • 1,786
  • 1
  • 17
  • 22