I read this question from stackoverflow:
How to post data in PHP using file_get_contents?
which covers pretty much everything in order to explain as to how to use file_get_contents()
function in php
To practice, I created these two php files :
1.php
<?php
$postdata = http_build_query(
array(
'name' => 'example',
'roll' => '123321'
)
);
$opts = array('http' =>
array(
'method' => 'post',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://localhost/2.php', false, $context);
echo $result;
?>
And this is my code for
2.php
<?php
$name = $_POST['name'];
$roll = $_POST['roll'];
echo $name . "<br>" . $roll;
?>
The expected output is that 1.php should send "name" and "roll" to 2.php using method="post"
and get the contents of that file and print them like this :
Expected Output :
Example
123321
And this is the output that i am getting now (as if no POST data is sent)
Notice: Undefined index: name in C:\xampp\htdocs\2.php on line 3
Notice: Undefined index: roll in C:\xampp\htdocs\2.php on line 4