我想构建一个RESTful API。我正在使用MAC,并且已安装XAMPP服务器。我有一个Python脚本,我想从PHP调用它。
我的PHP文件如下:
$output = shell_exec("python $pyfilepath $post->id $post->name");
echo $output;
而且没有问题。
我的python代码如下:
import sys
#import mysql.connector
def my_results(id,name):
print("My args are:",id,name)
if __name__ == '__main__':
my_results(sys.argv[1],sys.argv[2])
我可以读取参数并将其传递给python脚本,但是如果我尝试激活以下内容:
import mysql.connector
我的my_results定义没有任何结果。
我的所有内容都建立在python上,我只需要调用我的脚本即可。知道有什么问题吗?
谢谢
请尝试一下:
#!/usr/bin/env php
ob_start();
passthru("python $pyfilepath $post->id $post->name");
$output = ob_get_clean();
或尝试一下
$output = shell_exec(escapeshellcmd("python $pyfilepath $post->id $post->name"));
echo $output;
尝试一个简单的脚本test.py
#!/usr/bin/env php
print("hello")
并这样称呼它
$output = shell_exec(escapeshellcmd("test.py"));
echo $output;