[英]How can I call shell_exec() from PHP webhook?
I'm having a major issue and I can't find a solution. 我遇到了一个重大问题,我无法找到解决方案。 I currently have a PHP script that accepts a webhook (HTTP POST) as a trigger.
我目前有一个PHP脚本,它接受webhook(HTTP POST)作为触发器。 Based on variables contained in the POST, an exec('python script') is supposed to run, and return some variables.
基于POST中包含的变量,应该运行exec('python script')并返回一些变量。 It works fine when I run it from the command line it works fine, but when I try to trigger the script from any type of HTTP request, the exec() function does not run.
当我从命令行运行它工作正常时它工作正常,但是当我尝试从任何类型的HTTP请求触发脚本时,exec()函数不会运行。 Here is the section of code that is affected:
以下是受影响的代码部分:
$command = shell_exec('python C:\\xampp\\cgi-bin\\SECpull.py');
$company_dump = print_r($command, TRUE);
$fp = fopen('retvals.txt', 'w');
fwrite($fp, $company_dump);
fclose($fp);
If it isn't possible to have an exec() function in a PHP script that is triggerd by an HTTP POST, then how should I go about this? 如果在HTTP脚本触发的PHP脚本中不可能有exec()函数,那么我应该怎么做呢?
Problem 问题
As Robbie Averill mentioned, most probably you have a permission issue and you can fix it by adding additional permissions to user that is used to run the web server. 正如Robbie Averill所提到的,很可能您有权限问题,您可以通过向用户运行Web服务器添加其他权限来修复它。 However, I think it is not a good style to mix php with python , and if you cannot re-write
SECpull.py
to php , then better do everything in python . 但是,我认为将php与python混合起来并不是一个好的风格,如果你不能将
SECpull.py
重新写入php ,那么最好在python中做所有事情。
I suggest you to use Flask for that. 我建议你使用Flask 。
Sample 样品
# server.py
from flask import Flask
app = Flask(__name__)
# Setup custom route here
@app.route("/")
def index():
# Add SECpull.py logic here.
return "Welcome To Python Server!"
if __name__ == "__main__":
app.run()
Setup 设定
$ pip install Flask
$ python server.py
* Running on http://localhost:5000/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.