I am trying to pass the value of a variable from php to a python script.
php:
<?php
$item = array('warren');
$command = escapeshellcmd('hello.py $item');
$output = shell_exec($command);
echo $output;
?>
python:
#!/usr/bin/env python
import sys
input = sys.argv[1]
print input
This results in $item displayed. I've tried all manner of different quotation marks, but still can't get the actual value (warren) to be passed. How do I get the value stored in $item to get passed, not the literal?
I've also tried How to pass variable from PHP to Python?
Php:
<?php
$item = array('warren');
$output = shell_exec('hello.py' . $item);
echo $output;
?>
But this gives me error:
Notice: Array to string conversion in C:\wamp64\www\crud\clientdetails.php on line 613
2 Answers 2
You can do either,
$output = shell_exec('hello.py ' . implode(',', $item));
Then explode the string in Python or you could pass only value in the array,
$output = shell_exec('hello.py ' . $item[0])
Reading Material
Comments
The reason you are getting the error is because you are trying to pass an array as a string.
If you want to pass the data in $item. You would need to do:
$string;
foreach ($item as &$value)
{
$string .= $value . ",";
}
$output = shell_exec('hello.py' . $string);
Turns the $item array into a string which can then be passed into your python
$output = shell_exec('hello.py' . $item);$output = shell_exec('hello.py ' . $item[0])