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 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)