从python调用php函数 [英] call php function from python
本文介绍了从python调用php函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的PHP代码:
function start($height, $width) {
# do stuff
return $image;
}
这是我的Python代码:
Here my Python code:
import subprocess
def php(script_path):
p = subprocess.Popen(['php', script_path], stdout=subprocess.PIPE)
result = p.communicate()[0]
return result
page_html = "test entry"
output = php("file.php")
print page_html + output
imageUrl = start(h,w)
在Python中,我想使用该PHP启动函数.我不知道如何从Python访问启动函数.谁可以帮我这个事?
In Python I want to use that PHP start function. I don't know how to access start function from Python. Can anyone help me on this?
推荐答案
这就是我的方法.它像一种魅力.
This is how I do it. It works like a charm.
# shell execute PHP
def php(code):
# open process
p = Popen(['php'], stdout=PIPE, stdin=PIPE, stderr=STDOUT, close_fds=True)
# read output
o = p.communicate(code)[0]
# kill process
try:
os.kill(p.pid, signal.SIGTERM)
except:
pass
# return
return o
要执行特定文件,请执行以下操作:
To execute a particular file do this:
width = 100
height = 100
code = """<?php
include('/path/to/file.php');
echo start(""" + width + """, """ + height + """);
?>
"""
res = php(code)
这篇关于从python调用php函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文