使用curl将php文件的输出保存到服务器上的新文件

使用curl将php文件的输出保存到服务器上的新文件,php,session,curl,Php,Session,Curl,我的计划是创建php“模板”文件,该文件将根据需要为网站用户创建各种报告。我要做的是获取php文件的输出,并将其保存为服务器上的新文件。卷曲和文件创建工作正常,但是 问题和疑问: get_archive('http://www.test.com/template/output.php', '/home/test/public_html/reports/', 'newreport.html'); function get_archive($file, $local_path, $newfilen

我的计划是创建php“模板”文件,该文件将根据需要为网站用户创建各种报告。我要做的是获取php文件的输出,并将其保存为服务器上的新文件。卷曲和文件创建工作正常,但是

问题和疑问:

get_archive('http://www.test.com/template/output.php', '/home/test/public_html/reports/', 'newreport.html');

function get_archive($file, $local_path, $newfilename) 
{ 
    // if location does not exist create it
    if(!file_exists($local_path)) 
    {
        mkdir($local_path, 0755, true);
    }

    $out = fopen($local_path.$newfilename,"wb");
    if ($out == false){ 
      exit; 
    }

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_FILE, $out); 
    curl_setopt($ch, CURLOPT_URL, $file);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_exec($ch);

    curl_close($ch);

}
  • 使用curl时,我不再能够访问为登录用户设置的任何$\u会话变量。我需要能够在output.php文件中访问这些内容,以便在文件中实际创建所需的正确内容。是否有方法传递/允许访问$\u会话变量?如果没有,是否有一种方法可以将数据与curl一起发布。。。比如发布用户ID或者类似的东西

  • 对mkdir使用recursive=true时是否需要该模式

  • 最后。。。我最初的计划是将这些“模板”文件存储在public_html目录之外,因此无法直接访问这些文件。。。只有通过我的脚本,尽管看起来curl只能在给定url时保存php文件的输出。话虽如此,这些文件似乎必须可以通过网络访问。有没有办法给curl一个文件路径,比如“/home/test/template/output.php”,然后仍然处理该文件并保存它的实际输出

我支持除文件内容之外的任何建议,因为我已经禁用了fopen。在这种情况下,使用输出缓冲区会更好吗

创建报告的示例调用:

get_archive('http://www.test.com/template/output.php', '/home/test/public_html/reports/', 'newreport.html');

function get_archive($file, $local_path, $newfilename) 
{ 
    // if location does not exist create it
    if(!file_exists($local_path)) 
    {
        mkdir($local_path, 0755, true);
    }

    $out = fopen($local_path.$newfilename,"wb");
    if ($out == false){ 
      exit; 
    }

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_FILE, $out); 
    curl_setopt($ch, CURLOPT_URL, $file);
    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_exec($ch);

    curl_close($ch);

}
简单示例模板文件(output.php):这显然要广泛得多,但您应该明白这一点

<?php

// These files can be included only if INCLUDE_CHECK is defined
require '/home/test/public_html/custom/functions/connect.php';
require '/home/test/public_html/custom/functions/functions.php';
require '/home/test/public_html/custom/functions/session.php';

?>


<!DOCTYPE html>
<html>
<head></head>

<body>

    <div class="row">
        <div class="col-md-12">                                                         
                <?php

                    //create report contents
                                    echo $_SESSION['user']['account_id']; // returns nothing    

                ?>

        </div>
    </div>


</body>
</html>


这是一种不可靠的方法,但基本问题是您需要为curl请求设置cookies,以便在会话id中传递。您还建议采用什么其他方法?这是我第一次想到。。。我对任何事情都持开放态度。我每次都会动态地呈现它们,或者如果延迟很昂贵,只需将curl数据存储在文件中并动态地呈现报告。我明白你的意思,但我真的想使用一个模板系统来轻松地进行更改,而不是将所有内容都回送到文件中。我之所以保存该文件,是因为其目的是在单击时创建报告,然后允许用户下载该文件。你怎么认为?