将数据发送到file_get_contents

时间:2016-02-24 12:40:51

标签: php

根据有关此事的其他帖子,我的代码似乎很好,但是它不起作用。

$postdata = http_build_query($_SESSION);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context  = stream_context_create($opts);

$mailBody = file_get_contents('customorderemailtemplate.html', false, $context);

customorderemailtemplate.html中,我使用$_POST但它无效 - 我确信$_SESSION在将其传递给$postdata时不为空。

1 个答案:

答案 0 :(得分:0)

您似乎认为您的代码会向Web服务器创建HTTP请求,该Web服务器将处理POST数据,调用PHP并将值插入HTML。这不会发生,因为您的“URL”指向本地文件;它不以http://开头。所以文件内容将从磁盘中读取,就是这样。

如果它可以正常工作,那么以这种方式“渲染”模板也是一种相当疯狂的方式。你可能只是在寻找这个:

ob_start();
require 'customorderemailtemplate.html';
$mailBody = ob_get_clean();

customorderemailtemplate.html内,您可以使用<?php echo $_SESSION['blah']; ?>输出值。