[英]add post parameters to file_get_contents in php
I want to ask you how to add POST
parameter to php file in file_get_contents
function. 我想问你如何在file_get_contents
函数中向php文件添加POST
参数。 I tried something like this, but its not working: 我尝试了类似的方法,但是没有用:
$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();
In output is only 'content' and static html text, In 'templates/email/email_after_registration.php' I want also use $_POST['firstname']
, $_POST['lastname']
, $_POST['user_id']
, but they are empty :( . 在输出中仅是“内容”和静态html文本,在“ templates / email / email_after_registration.php”中,我还想使用$_POST['firstname']
, $_POST['lastname']
, $_POST['user_id']
,但它们是空的:(。
Different solutions: 不同的解决方案:
1) If you set second parameter as false
1)如果将第二个参数设置为false
Means that you are not using inclusion and you need the full path 表示您没有使用包容性,需要完整路径
file_get_contents('http://{HOST}/templates/email/email_after_registration.php', false, $context);
Have a look in detail HERE 在这里详细了解
2) Use inclusion instead? 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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.