【问题标题】:Edit and redirect a post request with json使用 json 编辑和重定向 post 请求
【发布时间】:2017-08-23 18:16:15
【问题描述】:

对于我的项目,我正在尝试在我的 index.php 上获取发布请求,使用一些随机值对其进行编辑,然后将其重定向到另一个页面。

我尝试了以下方法:

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

我的 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);

?>

通过这样做,我只能在我的回复中得到 [答案]。 我怎样才能重新编译完整的请求?

【问题讨论】:

  • 您在 SO 上有多少个帐户,@JohnSnow? stackoverflow.com/q/45829570/2943403 当我真正想帮助您时,您从其他问题中复制粘贴我的代码(不奖励我的努力甚至评论)似乎有点粗鲁。
  • 如果你对我的代码块有任何疑问,你应该问我——因为我写了它。

标签: php json regex


【解决方案1】:

看起来您只是未能重新组装所有零件。存储 POST,操作 answer 元素,然后替换该部分。

<?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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 2011-10-01
    • 1970-01-01
    相关资源
    最近更新 更多