drd94483 2014-03-24 10:29
浏览 43
已采纳

发送帖子数据和file_get_contents()[重复]

This question already has an answer here:

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

</div>
  • 写回答

1条回答 默认 最新

  • douji2283 2014-03-24 10:44
    关注

    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.”

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应