将帖子参数添加到php中的file_get_contents

时间:2016-03-04 12:16:11

标签: php

我想问你如何在POST函数中将file_get_contents参数添加到php文件中。我尝试过类似的东西,但它不起作用:

$getdata = http_build_query(
            array(
                'user_id' => $_SESSION['user_id'],
                'firstname'=>$firstname,
                'lastname' =>$lastname
            )
    );

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

    $context = stream_context_create($opts);

    $this->body = file_get_contents('templates/email/email_after_registration.php', false, $context);
    echo 'content' . $this->body;
    exit();

输出中只有'content'和静态html文本,在'templates / email / email_after_registration.php'中我也想使用$_POST['firstname']$_POST['lastname']$_POST['user_id'],但它们是空:(。

2 个答案:

答案 0 :(得分:0)

不同的解决方案:

1)如果您将第二个参数设置为false

  

表示您没有使用包含,而您需要完整路径

file_get_contents('http://{HOST}/templates/email/email_after_registration.php', false, $context);

详细了解HERE

2)改为使用包含?

$_POST['user_id'] = $_SESSION['user_id'];
$_POST['firstname'] = $firstname;
$_POST['lastname'] = $lastname;

ob_start();
include('templates/email/email_after_registration.php');
$this->body = ob_get_clean();
echo 'content' . $this->body;
exit();

答案 1 :(得分:0)

我倾向于使用cURL。