【问题标题】:Writing _rendered_ PHP to HTML file将 _rendered_ PHP 写入 HTML 文件
【发布时间】:2011-04-28 06:43:31
【问题描述】:

这里有问题。

我需要将 PHP 脚本的输出写入 HTML 页面,然后由 DOMPDF 处理,这样我就可以制作一个不错的小 PDF 供一些朋友使用。

当我使用 file_get_contents 来获取“捐助者”PHP 脚本的内容时,它确实做到了 - 字面意思。它会抓取未评估的 PHP,并让我随心所欲地进行操作。

我需要运行 PHP 脚本,然后将输出和 file_put_contents 放在某个地方。

谢谢, 抢

【问题讨论】:

    标签: php html


    【解决方案1】:

    通过http调用让它运行。

    file_get_contents("http://localhost/my/script/file.php");
    

    或者,从命令行运行它:

    php -f /path/to/my/script/file.php
    

    【讨论】:

      【解决方案2】:

      好吧,理想情况下,您可以配置 PHP 脚本,这样就不用编写其内容,而是构建一个巨大的字符串并返回它。这样你就可以简单地调用一个函数并返回一个包含你需要的信息的字符串。

      如果目标脚本超出您的控制范围,您可以通过以下方式解决它:

      ob_start();
      include('script.php');
      $contents = ob_get_contents();
      ob_end_clean();
      

      【讨论】:

      • 优雅。简单的。其他我不知道的东西。谢谢,投票!
      【解决方案3】:

      您可以通过几种不同的方式做到这一点:

      1) 通过它的 URL(通过网络浏览器)而不是文件路径来获取脚本。

      2) 使用eval

      3) 使用output buffering 并将脚本包含到当前脚本中。示例:

      ob_start();
      include('script.php');
      $result = ob_get_clean();
      

      4) 使用shell_exec('php -f script.php');从命令行运行它

      【讨论】:

        【解决方案4】:

        两个快速解决方案。首先是设置 PHP 脚本从 url 运行,然后用户 file_get_contentscurl 函数下载页面。这将为您提供一个内容正确的字符串。

        $string = file_get_contents('http://example.com/myscript.php');
        

        第二种是使用include和输出缓冲。输出缓冲采用任何echoprint 语句,并将它们的内容存储在内存中,而不是输出到浏览器/屏幕。

        ob_start(); //start output buffering
        include("myscript.php"); //script will run in the current context, but output will be stored in memory
        $string = ob_get_clean(); //stored output will be fetched into $string, and buffering ended
        

        【讨论】:

          猜你喜欢
          • 2021-04-19
          • 2013-01-31
          • 2015-03-15
          • 1970-01-01
          • 2011-06-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多