使用shell_exec从PHP内调用Python [英] Calling Python from within PHP using shell_exec

查看:268
本文介绍了使用shell_exec从PHP内调用Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的默认Web应用程序基于PHP.但是,为了简便起见,我构建了一个进行一些分析的python脚本.现在,我需要php来调用python代码并检索python代码提供的输出.这两个文件都在同一服务器上,但是不在同一文件夹中.我目前无法使用的方法如下:

My default Web-Application is based on PHP. However, for easiness, I have built a python script that does some analysis. Now I need the php to call the python code and retrieve the output that the python code delivers. Both files are in the same server, not on the same folder however. My current approach, which does not work, looks as following:

$cmd = "/usr/bin/python /var/www/include/sCrape.py -u '$my_url' ";
$response = shell_exec($cmd);
$response = json_decode($response, true);

现在,当我尝试打印$ response时,我得到了NULL对象(它应该返回一个数组字符串,应该通过json_decode对其进行解码).我的功能有问题吗? php位于/var/www/html/中.当两个文件位于同一目录中时,以下代码将起作用:

Now when I try to print out $response, I get the NULL object (it should return an array-string that I should decode through json_decode). Am I doing something wrong with the function? The php is located within /var/www/html/. The following code works when both files are in the same directory:

$cmd = "python sCrape.py -u '$my_url'";
$response = shell_exec($cmd);
$response = json_decode($response, true);

更多信息:my_url是从php输入的经过净化的$ _POST变量输入的,但是我现在试图完全禁用净化以测试它是否可以工作,但是仍然没有(仍然是url的路径,直到到达函数更长,因为它必须通过form-post传递,而在我的同一文件夹测试例中,我只是声明了$ my_url). -u后缀表示脚本输入了一个URL.

Further information: my_url is input as a sanitized $_POST variable from php, but I have now tried to completely disable sanitizing to test if it would work, but it still didn't (still, the url's path until it reaches the function is longer, as it must be passed on through form-post, whereas in my same-folder-test-case I have simply just declared $my_url). the -u postfix means that the script inputs a url.

非常感谢您!

推荐答案

您是否尝试在shell中运行/usr/bin/python /var/www/include/sCrape.py -u '$my_url'?错误可能在那里.

Did you try running /usr/bin/python /var/www/include/sCrape.py -u '$my_url' in a shell? The mistake is probably there.

尝试:

$cmd = "/usr/bin/python /var/www/include/sCrape.py -u '$my_url' 2>&1";
$response = shell_exec($cmd);
echo $response;

这应该输出一条错误消息.

This should output an error message.

shell_exec仅返回标准输出(stdout)的输出,如果发生错误,则会将其写为标准错误"(stderr). 2>&1将stderr重定向到stdout.参见在外壳中,"2>& 1"是什么意思?.

shell_exec only returns output from the standard output (stdout), if an error occurs it's written "to the" standard error (stderr). 2>&1 redirects stderr to stdout. See In the shell, what does " 2>&1 " mean?.

您可能希望在python脚本的第一行添加#!/usr/bin/env python并使其可执行chmod +x /var/www/include/sCrape.py.之后,您应该能够运行脚本而无需显式调用python. /var/www/include/sCrape.py -u '$my_url'

You may want to add #!/usr/bin/env python on the first line of your python script and make it executable chmod +x /var/www/include/sCrape.py. Afterwards you should be able to run your script without explicitly calling python. /var/www/include/sCrape.py -u '$my_url'

这篇关于使用shell_exec从PHP内调用Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆