在php中将post参数添加到文件中

在php中将post参数添加到文件中,php,Php,我想问您如何在file\u get\u contents函数中将POST参数添加到php文件中。我试过类似的方法,但不起作用: $getdata = http_build_query( array( 'user_id' => $_SESSION['user_id'], 'firstname'=>$firstname, 'lastname' =>$lastnam

我想问您如何在
file\u get\u contents
函数中将
POST
参数添加到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();
在“templates/email/email\u after\u registration.php”中,我还想使用
$\u POST['firstname']
$\u POST['lastname']
$\u POST['user\u id']
,但它们是空的:(.

不同的解决方案:

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

这意味着您没有使用包含,您需要完整的路径

细看

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();

我倾向于使用cURL

$_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();