1

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

Community
  • 1
  • 1
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71

1 Answers1

4

http://www.php.net/manual/de/context.http.php#101933:

“watch your case when using methods (POST and GET)...it must be always uppercase. in case of you write it in lower case it wont work.”

CBroe
  • 91,630
  • 14
  • 92
  • 150