如何在Python中包含PHP脚本?

时间:2022-02-16 07:07:16

I have a PHP script (news-generator.php) which, when I include it, grabs a bunch of news items and prints them. Right now, I'm using Python for my website (CGI). When I was using PHP, I used something like this on the "News" page:

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

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

(I cut down the example for simplicity.)

(为简单起见,我减少了这个例子。)

Is there a way I could make Python execute the script (news-generator.php) and return the output which would work cross-platform? That way, I could do this:

有没有办法让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

5 个解决方案

#1


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

#2


PHP is a program. You can run any program with subprocess.

PHP是一个程序。您可以使用子进程运行任何程序。

The hard part is simulating the whole CGI environment that PHP expects.

困难的部分是模拟PHP期望的整个CGI环境。

#3


maybe off topic, but if you want to do this in a way where you can access the vars and such created by the php script (eg. array of news items), your best best will be to do the exec of the php script, but return a json encoded array of items from php as a string, then json decode them on the python side, and do your html generation and iteration there.

也许是偏离主题,但如果你想以一种可以访问vars的方式来执行此操作,例如由php脚本创建的(例如,新闻项目数组),那么最好的方法就是执行php脚本的exec,但是从php返回一个json编码的项目数组作为字符串,然后json在python端解码它们,并在那里进行html生成和迭代。

#4


I think the best answer would be to have apache render both pages separately and then use javascript to load that page into a div. You have the slight slowdown of the ajax load but then you dont have to worry about it.

我认为最好的答案是让apache分别渲染两个页面,然后使用javascript将该页面加载到div中。你有ajax负载的轻微减速但你不必担心它。

There is an open-source widget thing that will run multiple languages in 1 page but I cant remember what its called.

有一个开源小部件的东西,将在1页中运行多种语言,但我不记得它的名称。

#5


You could use urllib to get the page from the server (localhost) and execute it in the right environment for php. Not pretty, but it'll work. It may cause performance problems if you do it a lot.

您可以使用urllib从服务器(localhost)获取页面并在适当的PHP环境中执行它。不漂亮,但它会起作用。如果你做了很多,可能会导致性能问题。

#1


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

#2


PHP is a program. You can run any program with subprocess.

PHP是一个程序。您可以使用子进程运行任何程序。

The hard part is simulating the whole CGI environment that PHP expects.

困难的部分是模拟PHP期望的整个CGI环境。

#3


maybe off topic, but if you want to do this in a way where you can access the vars and such created by the php script (eg. array of news items), your best best will be to do the exec of the php script, but return a json encoded array of items from php as a string, then json decode them on the python side, and do your html generation and iteration there.

也许是偏离主题,但如果你想以一种可以访问vars的方式来执行此操作,例如由php脚本创建的(例如,新闻项目数组),那么最好的方法就是执行php脚本的exec,但是从php返回一个json编码的项目数组作为字符串,然后json在python端解码它们,并在那里进行html生成和迭代。

#4


I think the best answer would be to have apache render both pages separately and then use javascript to load that page into a div. You have the slight slowdown of the ajax load but then you dont have to worry about it.

我认为最好的答案是让apache分别渲染两个页面,然后使用javascript将该页面加载到div中。你有ajax负载的轻微减速但你不必担心它。

There is an open-source widget thing that will run multiple languages in 1 page but I cant remember what its called.

有一个开源小部件的东西,将在1页中运行多种语言,但我不记得它的名称。

#5


You could use urllib to get the page from the server (localhost) and execute it in the right environment for php. Not pretty, but it'll work. It may cause performance problems if you do it a lot.

您可以使用urllib从服务器(localhost)获取页面并在适当的PHP环境中执行它。不漂亮,但它会起作用。如果你做了很多,可能会导致性能问题。