将 PHP 页面的 html 响应返回到变量中 [英] Return html response from PHP page in to variable

查看:39
本文介绍了将 PHP 页面的 html 响应返回到变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用通过另一个 PHP 文件创建的一些 HTML 生成一封电子邮件.

I am trying to generate an email with some HTML that is created via another PHP file.

电子邮件生成文件由每小时运行的 cron 运行.存在另一个文件,该文件生成电子邮件所需的 HTML.

Email generating file is run by a cron running every hour. Another file exists that generates the HTML required for the email.

HTML 生成文件没有我可以调用的函数,例如:

The HTML generating file does not have a function that I can call, for instance:

$emailBody = generateHTML($id);

HTML 生成文件旨在包含在您希望显示 HTML 的页面上,例如:

The HTML generating file was designed to be included on a page that you wished to display the HTML, for instance:

include "htmlGenerator.php";

我的问题是:我怎样才能将 htmlgenerator.php 文件返回到变量中,准备好推送到电子邮件中.

My question is: How can I get what the htmlgenerator.php file returns in to a variable, ready to be pushed in to an email.

抱歉,如果这不是很清楚,我很乐意回答任何问题.

Apologies if this is not very clear, I will be happy to answer any questions.

提前致谢!

推荐答案

如果我明白你说的,你可以使用输出缓冲,这样就可以得到 htmlGenerator.php

If I understood what you said, you can use output buffering, so that you can get the output of htmlGenerator.php

例如:

ob_start();
// as an example
echo "Hello World";
// or in our case
include "htmlGenerator.php";
$result = ob_get_contents();
ob_end_clean();

你也可以像这样创建一个简单的函数:

You can also create a simple function like this:

function include_output($filename)
{
    ob_start();
    include $filename;
    $contents = ob_get_contents();
    ob_end_clean();
    return $contents;
}

$mycontent = include_output("htmlGenerator.php");

阅读 php 文档了解更多详情.

Read the php documentation for further details.

这篇关于将 PHP 页面的 html 响应返回到变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆