Linux 我可以从 php 调用 python 脚本或函数吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6094643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:13:04  来源:igfitidea点击:

Can I call python script or function from php

phppythonlinux

提问by Mosquito

I have the php page which lists the backups with filename and username. the backups are done with python script but for web interface I used php.

我有一个 php 页面,其中列出了带有文件名和用户名的备份。备份是用 python 脚本完成的,但对于 Web 界面,我使用了 php。

Now I want to put the facility on webpage where there will be button called restore next to backup file name and all the home dir contents are replaced.

现在我想将该工具放在网页上,其中备份文件名旁边将有一个名为还原的按钮,并且所有主目录内容都将被替换。

I can make the python function with parameters like backup filename , username and it restores the backup.

我可以使用备份文件名、用户名等参数创建 python 函数,并恢复备份。

But I don't know how can I pass variables to python via php

但我不知道如何通过 php 将变量传递给 python

采纳答案by vkrams

Yes you can call exec("python pythonscript.py someparams");or if you need to save the output to a variable use this code

是的,您可以调用,exec("python pythonscript.py someparams");或者如果您需要将输出保存到变量,请使用此代码

exec("python mypythonscript.py someparams",$output);
var_dump($output);

More informaiton: call python script by php and solve errors

更多信息:通过php调用python脚本并解决错误

回答by Rudie

You can call the shell from PHP:

您可以从 PHP 调用 shell:

and there are more.

还有更多。