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.

4

1 回答 1

0

您可以尝试使用exec功能。它还执行命令执行,但与system不同,它不会将内容输出到标准输出。唯一的缺点是返回是标准输出中每一行的数组(不是字符串,如system。您也可以尝试使用proc_open,它允许您将输出重定向到任意管道。

于 2013-06-20T07:01:43.560 回答