简体   繁体   English

在Python中执行php代码

[英]Execute php code in Python

For some reason, I have to run a php script to get an image from Python. 由于某些原因,我必须运行一个php脚本才能从Python获取图像。 Because the php script is very big and it is not mine, it will takes me days to find out the right algorithm used and translate it into python. 因为php脚本很大,而且不是我的,所以我需要几天的时间才能找到所使用的正确算法并将其转换为python。

I am wonder if there is any way to run php script, with few parameters, which returns a image, in python. 我想知道是否有任何方法可以运行带有少量参数的php脚本,以返回python中的图像。

Example code: 示例代码:

import subprocess

# if the script don't need output.
subprocess.call("php /path/to/your/script.php")

# if you want output
proc = subprocess.Popen("php /path/to/your/script.php", shell=True, stdout=subprocess.PIPE)
script_response = proc.stdout.read()

You can simply execute the php executable from Python. 您可以简单地从Python执行php可执行文件。

Edit: example for Python 3.5 and higher using subprocess.run : 编辑:使用subprocess.run Python 3.5及更高版本的示例:

import subprocess

result = subprocess.run(
    ['php', 'image.php'],    # program and arguments
    stdout=subprocess.PIPE,  # capture stdout
    check=True               # raise exception if program fails
)
print(result.stdout)         # result.stdout contains a byte-string

You can use php.py . 您可以使用php.py。 This would allow you to execute php code in python, like in this example (taken from here ): 这样,您就可以在python中执行php代码,例如以下示例(从此处获取 ):

php = PHP("require '../code/private/common.php';")
code = """for ($i = 1; $i <= 10; $i++) { echo "$i\n"; }"""
print php.get_raw(code)

Make a wrapper around the PHP script, which: 对PHP脚本进行包装,该脚本包括:

  • performs the stuff (If I understand well, it's an image creation), 执行这些工作(如果我理解得很好,那就是图像创作),
  • then redirects ( 301 Moved Permanently ) to the result image, 然后重定向(“ 永久移动301” )到结果图片,
  • (also, the image should be purged some day). (同样,应该在某天清除图像)。

So you can refer to this service (the PHP script) with a simple HTTP request, from anywhere, you can test it with browser, use from Python prg, you need just download the image the usual way. 因此,您可以通过一个简单的HTTP请求来引用此服务 (PHP脚本),您可以在任何地方使用浏览器对其进行测试,也可以在Python prg中使用,您只需按照通常的方式下载该图像即可。

Also, if you have a such standalone sub-system, don't feel bad about implement it with different language/technique. 另外,如果您有一个这样的独立子系统,那么不要以其他语言/技术来实现它。 It has several advantages, eg you can install that service on a different host. 它具有多个优点,例如,您可以将该服务安装在其他主机上。

Recommended reading: Service-Oriented Architecture on Wikipedia. 推荐阅读:Wikipedia上的面向服务的体系结构

If you can run the PHP script locally from the command-line, subprocess.check_output() will let you can PHP and will capture the return value. 如果可以从命令行本地运行PHP脚本,则subprocess.check_output()将使您可以使用PHP并捕获返回值。

If you are accessing PHP via a socket, then you can use urllib.urlopen() or urllib.urlretrieve() to pull down the resource. 如果要通过套接字访问PHP,则可以使用urllib.urlopen()urllib.urlretrieve()提取资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM