1

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

6
  • Possible duplicate of How to pass variable from PHP to Python? Commented Sep 13, 2017 at 15:54
  • I have tried this too, see edit Commented Sep 13, 2017 at 16:00
  • What is line 613? Commented Sep 13, 2017 at 16:01
  • $output = shell_exec('hello.py' . $item); Commented Sep 13, 2017 at 16:01
  • $output = shell_exec('hello.py ' . $item[0]) Commented Sep 13, 2017 at 16:03

2 Answers 2

1

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

implode

Sign up to request clarification or add additional context in comments.

Comments

0

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

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.