0

I am executing a Python script in PHP using system(). For me to get the result of my Python script, I use print command and catch the result in PHP. Here's my code:

Python (test.py)

import sys
name = sys.argv[1]
print 'Your name is ' + name

PHP

$result = system('python test.py John');
echo $result;

/* PHP Output */
Your name is John
Your name is John

As you can see, the output is doubled. The first one was generated by the Python script itself, the second one was because of echo command. Is there a way on how to avoid this doubled output? I just wanted to catch the result and will use it somewhere on my PHP script.

**NOTE: Just wondering if there is another way on how to pass Python script output to PHP a variable. My only intention here is to put the output on a PHP variable.

kimbebot
  • 887
  • 5
  • 16
  • 28
  • http://stackoverflow.com/questions/7765473/error-on-using-exec-to-call-python-script – RST Jun 20 '13 at 07:02

1 Answers1

0

You can try with exec function. It also performs a command execution but, at diference fo system, doesn't output the content to standard output. The only drawback is that the return is an array of every line in the stadard output (not a string, like system. You can also try with proc_open, that allows you redirect the output to an arbitrary pipe.

NDRange
  • 16
  • 2