dongmeba4877 2017-03-05 06:29
浏览 56
已采纳

不工作:使用file_get_contents发布数据

I am learning and I am trying post data to 4.php and get the result to printed on range.php.. I have already seen the How to post data in PHP using file_get_contents?

<?php
error_reporting(-1);

$postdata = http_build_query(
array('var1' => 'sugumar',
    'var2' => 'doh')
);

$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/php/4.php', false, $context);
 print_r($result);//DOESN'T PRINT ANYTHING
?>

4.php

 print_r($_REQUST);
$postdata = http_build_query($_POST);
print_r($postdata);

I want to know why it's not printing anything... please help me..

  • 写回答

1条回答 默认 最新

  • dtn51137 2017-03-05 07:10
    关注

    I have updated your code using code from the PHP manual and changes like adding PHP tags to 4.php and fixing typo in $_REQUEST.

    Here is the updated code, which seems to be working for me:

    <?php
    
    $data = array ('foo' => 'bar', 'bar' => 'baz');
    $data = http_build_query($data);
    
    $context_options = array (
            'http' => array (
                'method' => 'POST',
                'header'=> "Content-type: application/x-www-form-urlencoded
    "
                    . "Content-Length: " . strlen($data) . "
    ",
                'content' => $data
                )
            );
    
    $context = stream_context_create($context_options);
     $result = file_get_contents('http://localhost/php/4.php', false, $context);
     var_dump($result);//prints something :)
    ?>
    

    4.php

    <?php
    
     print_r($_REQUEST);
    $postdata = http_build_query($_POST);
    print_r($postdata);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错