如何在php中嵌入python脚本?

如何在php中嵌入python脚本?,php,python,interface,Php,Python,Interface,我知道在StackOverflow上也有类似的问题,但我都试过了,没有一个有效。在我的笔记本电脑上,我有一个Apache服务器,一个用PHP和Python脚本构建的网站 尝试: (一) 2) 和3)和$temp=exec($command,$output) php: python: import sys, json def tests():     b = 2     print("inside test")     print(b)     a = 4     return a #if _

我知道在StackOverflow上也有类似的问题,但我都试过了,没有一个有效。在我的笔记本电脑上,我有一个Apache服务器,一个用PHP和Python脚本构建的网站

尝试:

(一)

2) 和3)和$temp=exec($command,$output)

php:

python:

import sys, json

def tests():
    b = 2
    print("inside test")
    print(b)
    a = 4
    return a

#if __name__ == "__main__":

c = tests()
print("main")
print(c)
# Generate some data to send to PHP
result = {'status': 'Yes!'}

# Send it to stdout (to PHP)
print json.dumps(result)
4) apache配置已设置为:

AddHandler cgi-script .cgi .py 
exec()对我来说很好

murftown$ php -a
Interactive shell

php > $output = exec('python myscript.py', $output);
php > echo $output;
{"status": "Yes!"}
php > print_r(json_decode($output));
stdClass Object
(
    [status] => Yes!
)
这是在交互式控制台中,下面是纯php:

$output = exec('python myscript.py', $output);
print_r(json_decode($output));
exec()对我来说很好

murftown$ php -a
Interactive shell

php > $output = exec('python myscript.py', $output);
php > echo $output;
{"status": "Yes!"}
php > print_r(json_decode($output));
stdClass Object
(
    [status] => Yes!
)
这是在交互式控制台中,下面是纯php:

$output = exec('python myscript.py', $output);
print_r(json_decode($output));