dtukyb8095 2017-08-23 18:16
浏览 69

使用json编辑和重定向发布请求

For my project, i'm trying to get the post request on my index.php, edit it with some random values, and then redirect it to another page.

I tried the following:

----------- POST REQUEST -----------

Array
(
    [authToken] => 0a65e943412453ecec35c814
    [sessionId] => 431503466924
    [answers] => [{"Boost":false,"answerTime":1300,"id":3},{"Boost":false,"answerTime":800,"id":1},{"Boost":false,"answerTime":900,"id":3},{"Boost":false,"answerTime":1000,"id":1},{"Boost":false,"answerTime":1200,"id":1}]
    [userId] => 2235
)

----------- POST REQUEST -----------

My index.php

<?php
$time=[800,900,1000,1100,1200,1300,1500];
$array = json_decode($_POST['answers'], true);
foreach($array as &$k)
{
    $k['answerTime'] =$time[array_rand($time)];
}

$postpop = json_encode($array);

$url = 'http://127.0.0.1/index2.php';

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($postpop));
curl_setopt($ch,CURLOPT_POSTFIELDS,$postpop);

$result = curl_exec($ch);

curl_close($ch);

?>

By doing that, i only get the [answers] on my response. How can i get to recompile the full request?

  • 写回答

1条回答 默认 最新

  • dqoag62688 2017-08-23 18:21
    关注

    It looks like you are simply failing to reassemble all the parts. Store the POST, manipulate the answer element, and then replace that part.

    <?php
    
    $time = [800,900,1000,1100,1200,1300,1500];
    
    //Store the full post as received.
    $originalPost = $_POST;
    
    $array = json_decode($_POST['answers'], true);
    foreach($array as &$k) {
        $k['answerTime'] = $time[array_rand($time)];
    }
    
    //replace just the part of the array we manipulated
    $originalPost['answers'] = json_encode($array);
    
    $url = 'http://127.0.0.1/index2.php';
    
    $ch = curl_init();
    
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_POST,count($originalPost));
    curl_setopt($ch,CURLOPT_POSTFIELDS,$originalPost);
    
    $result = curl_exec($ch);
    
    curl_close($ch);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Fluent udf 编写问题
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系
  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突