如何从PHP中的Python脚本(带有参数)执行函数? [英] How to execute a function from a Python script (with parameters) in PHP?
问题描述
我最近一直在使用PHP进行网络搜寻器的开发,但作为最新的PHP编码器,我不是最高级的.我也知道一些Python,我可以在Python中做一些事情,而在PHP中却不行.
I've recently been working on a web crawler with PHP and as a newish PHP coder, I'm not the most advanced. I also know a decent bit of Python and there are a few things I am able to do in Python yet not in PHP.
我有什么办法可以在PHP脚本中运行带有参数的Python函数?请具体回答,因为我对PHP并不感到惊讶.
Is there any way for me to run a Python function with parameters inside a PHP script? Please be specific in your answers as I'm not amazing at PHP.
推荐答案
使用php的 shell_exec 函数
将shell_exec
视为在终端中基本正在运行的命令.因此,如果您通常像这样从外壳程序运行python脚本:
Think of shell_exec
as basically running commands in your terminal. So if you normally run your python script from the shell like this:
/python-scripts/myscript.py "here is an argument"
您将把相同的命令传递给shell_exec
,如下所示:
You would pass the same command into shell_exec
, like this:
$output = shell_exec("python-scripts/myscript.py 'here is an argument'");
注意:shell_exec
将返回您运行的命令的输出,因此,如果要在PHP脚本中使用python程序的输出,请将其保存到变量中.
Note: shell_exec
will return the output of the command you run, so save it to a variable if you want to use the output from the python program in your PHP script.
:修复了语法问题,提供了更具体的示例
Edits: fixed syntax issues, provided a more specific example
这篇关于如何从PHP中的Python脚本(带有参数)执行函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!