python调用PHP

2020-01-06 11:14:26 浏览数 (1)

. 调用php

方法一:

代码语言:javascript复制
import subprocess

#simple caller, disguard output

subprocess.call("php /path/to/my/old/script.php")

# if you want output

proc = subprocess.Popen("php /path/to/my/script.php", shell=True,
stdout=subprocess.PIPE)

script_response = proc.stdout.read()
代码语言:javascript复制
方法二:
代码语言:javascript复制
# 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:

代码语言:javascript复制
width = 100
height = 100

code = """<?php

  include('/path/to/file.php');
  echo start("""   width   """, """   height   """);

?>
"""
res = php(code)

0 人点赞