简体   繁体   English

如何获取html文件而不是包含文件的html生成内容

[英]How to get the html generated contents of a php file instead of include the file

I want to make a cms which generates html pages and creates files to store each page. 我想制作一个可生成html页面并创建用于存储每个页面的文件的cms。

Ideally i need something like that: 理想情况下,我需要这样的东西:

<?php
$file1 = get_produced_html_from('mainPage.php');
/* write the file to a directory*/


$file2 = get_produced_html_from('ProductsPage.php');
/* write the file to a directory*/
?>

Is there any function i missed instead of require, include, require_once, include_onse etc? 我有没有错过任何功能,而不是require,include,require_once,include_onse等?

To clarify: I don't need the php code inside the .php file. 需要说明的是:我不需要.php文件中的php代码。 I need the html content only which means that the php file should be executed first. 我只需要html内容,这意味着应该首先执行php文件。

Do you believe the solution is something like using http://php.net/manual/en/function.file-get-contents.php by reading http://domain.com/templates/mainPage.php as a html stream? 您是否认为解决方案类似于通过读取http://domain.com/templates/mainPage.php作为html流来使用http://php.net/manual/en/function.file-get-contents.php

Thank you a lot. 非常感谢。

You need to catch the output from the buffer. 您需要捕获缓冲区的输出。

Here is a piece of code I wrote for somebody to demonstrate a very simple view renderer class. 这是我为某人编写的一段代码,用于演示一个非常简单的视图渲染器类。

public function render($file) {
    $file = $this->viewPath = APP_ROOT . 'View' . DS . $file . '.php';
    if (is_file($file)) {
        ob_start();
        extract($this->_vars);
        include($file);
        $content = ob_get_contents();
        ob_end_clean();
    } else {
        throw new RuntimeException(sprintf('Cant find view file %s!', $file));
    }

    return $content;
}

It turns on the output buffer ( ob_start() ) executes the php file and sets the variables and then gets the buffer ( ob_get_contents ()) and then it cleans the buffer for the next operation ( ob_end_clean() ). 它打开输出缓冲区( ob_start() )执行php文件并设置变量,然后获取缓冲区( ob_get_contents ()),然后清除缓冲区以进行下一个操作( ob_end_clean() )。 You can also use ob_end_flush() to directly clean and send the buffer. 您也可以使用ob_end_flush()直接清除并发送缓冲区。 I would not do that and instead do a proper shutdown process of the app and ensure everything is done and was done right without errors before sending the page to the client. 我不会这样做,而是对应用程序进行正确的关闭过程,并确保在将页面发送到客户端之前,一切都已正确完成且没有错误。

I think I'm going to make the whole code available on Github soon. 我想我很快将在Github上提供整个代码。 I'll update the answer then. 然后,我将更新答案。

You can just use cURL to get the whole rendered output from an url. 您可以只使用cURL从url获取整个呈现的输出。

You can use it like this: 您可以像这样使用它:

// Initiate the curl session
$ch = curl_init();

// Set the URL
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/mypage.php');

// Allow the headers
curl_setopt($ch, CURLOPT_HEADER, true);

// Return the output instead of displaying it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Execute the curl session
$output = curl_exec($ch);

// Close the curl session
curl_close($ch);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM