php - 如何在 Python 中包含 PHP 脚本?

标签 php python scripting integration execution

我有一个 PHP 脚本 (news-generator.php),当我包含它时,它会抓取一堆新闻条目并打印出来。现在,我正在为我的网站 (CGI) 使用 Python。当我使用 PHP 时,我在“新闻”页面上使用了这样的东西:

<?php
print("<h1>News and Updates</h1>");
include("news-generator.php");
print("</body>");
?>

(为了简单起见,我删减了示例。)

有没有办法让 Python 执行脚本 (news-generator.php) 并返回可跨平台工作的输出?这样,我可以这样做:

page_html = "<h1>News and Updates</h1>"
news_script_output = php("news-generator.php") //should return a string
print page_html + news_script_output

最佳答案

import subprocess

def php(script_path):
    p = subprocess.Popen(['php', script_path], stdout=subprocess.PIPE)
    result = p.communicate()[0]
    return result

# YOUR CODE BELOW:
page_html = "<h1>News and Updates</h1>"
news_script_output = php("news-generator.php") 
print page_html + news_script_output

关于php - 如何在 Python 中包含 PHP 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1060436/

相关文章:

python - 从 Python 字典中弹出许多元素的最佳方法

perl - 什么会导致 Perl 转储核心?

php - 使用属性序列化 XML 文档

php - Laravel 5 获取数组的输入值

php - 订阅处理

Python "if modified since"检测?

python - 简化python中的循环

javascript - jscript - 获取目录中的文件列表

macos - 查找素数 - 在 MacOS 上找不到 "factor"命令

javascript - PHP 中传入 http 请求的识别