首页 > 解决方案 > 如何从 Python 运行 php 代码字符串?

问题描述

我发现您可以使用以下命令从 Python 运行 php 文件:

import subprocess

proc = subprocess.Popen('php.exe input.php', shell=True, stdout=subprocess.PIPE)
response = proc.stdout.read().decode("utf-8")
print(response)

但是有没有办法从字符串而不是文件中运行 php 代码?例如:

<?php
  $a = ['a', 'b', 'c'][0];
  echo($a);
?>

标签: phppython

解决方案


[编辑]

php -r "code"subprocess.Popen一起使用:

def php(code):
    p = subprocess.Popen(["php", "-r", code],
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    out = p.communicate() #returns a tuple (stdoutdata, stderrdata)
    if out[1] != b'': raise Exception(out[1].decode('UTF-8'))
    return out[0].decode('UTF-8')

code = """ \
  $a = ['a', 'b', 'c'][2]; \
  echo($a);"""
print(php(code))

[原答案]

我找到了一个简单的类,可以让你做到这一点。
代码是不言自明的。该类包含3个方法:

  • get_raw(self, code):给定一个代码块,调用代码并将原始结果作为字符串返回
  • get(self, code):给定一个发出 json 的代码块,调用代码并将结果解释为 Python 值。
  • get_one(self, code):给定一个发出多个 json 值(每行一个)的代码块,产生下一个值。

您编写的示例如下所示:

php = PHP()
code = """ \
  $a = ['a', 'b', 'c'][0]; \
  echo($a);"""
print (php.get_raw(code))

您还可以在代码中添加前缀和后缀PHP(prefix="",postfix"")

PS.:我修改了原来的类,因为 popen2 已被弃用。我还使代码与 Python 3 兼容。你可以在这里得到它

import json
import subprocess

class PHP:
    """This class provides a stupid simple interface to PHP code."""

    def __init__(self, prefix="", postfix=""):
        """prefix = optional prefix for all code (usually require statements)
        postfix = optional postfix for all code
        Semicolons are not added automatically, so you'll need to make sure to put them in!"""
        self.prefix = prefix
        self.postfix = postfix

    def __submit(self, code):
        code = self.prefix + code + self.postfix
        p = subprocess.Popen(["php","-r",code], shell=True,
                  stdin=subprocess.PIPE, stdout=subprocess.PIPE)
        (child_stdin, child_stdout) = (p.stdin, p.stdout)
        return child_stdout

    def get_raw(self, code):
        """Given a code block, invoke the code and return the raw result as a string."""
        out = self.__submit(code)
        return out.read()

    def get(self, code):
        """Given a code block that emits json, invoke the code and interpret the result as a Python value."""
        out = self.__submit(code)
        return json.loads(out.read())

    def get_one(self, code):
        """Given a code block that emits multiple json values (one per line), yield the next value."""
        out = self.__submit(code)
        for line in out:
            line = line.strip()
            if line:
                yield json.loads(line)

推荐阅读