【问题标题】:How do I turn a POSTed JSON value into PHP?如何将 POSTed JSON 值转换为 PHP?
【发布时间】:2013-01-24 23:18:45
【问题描述】:

作为测试,此 JSON 数据正在发布到我的网站:

{
    "order": {
        "id": null,
        "created_at": null,
        "status": "new",
        "total_btc": {
            "cents": 100000000,
            "currency_iso": "BTC"
        },
        "total_native": {
            "cents": 2263,
            "currency_iso": "USD"
        },
        "custom": "123456789",
        "button": {
            "type": "buy_now",
            "name": "Test Item",
            "description": null,
            "id": null
        },
        "transaction": {
            "hash": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
            "confirmations": 0
        }
    }
}

由于它是从服务器到服务器发送的,因此我无法看到数据。我尝试将 $_POST 数组发送到文本文件,但它出现空白。我认为我需要做的是:

$data = json_decode($jsonData);

但是如何设置变量 $jsonData?

【问题讨论】:

  • 是否必须为json?为什么不发送通用 uri 样式的 & 分隔数据?
  • 问题不清楚,发送这个是什么以及发送到哪个页面?如果它是由 PHP 发送的,则必须在屏幕上回显 json 数据。
  • 要查看$_POST[] 数组,只需使用<?php print_r($_POST); ?>

标签: php json api post get


【解决方案1】:

您可以尝试使用wrappers 来读取原始 POST 查询。

$data = file_get_contents("php://input");

【讨论】:

  • 我之前确实试过这个,但我不记得结果了。我会上传并重试。
  • $data = json_decode(file_get_contents('php://input'), TRUE); file_put_contents('coinbase.txt', $data);打印数组
  • 别着急。看看你得到了什么 $data = file_get_contents("php://input"); file_put_contents('post.log', $data);
  • 你太棒了,做到了!我不知道我是怎么错过尝试的,但这是关键。
  • $data = json_decode(file_get_contents('php://input'), TRUE); $text = print_r($data,true); file_put_contents('coinbase.txt', $text);
【解决方案2】:

你有没有尝试过,将获得的字符串存储到一个变量中,然后对其进行解码?

$postedJsonData= '{"order":{"id":null,"created_at":null,"status":"new","total_btc":
{"cents":100000000,"currency_iso":"BTC"},"total_native":
{"cents":2263,"currency_iso":"USD"},"custom":"123456789","button":
{"type":"buy_now","name":"Test Item","description":null,"id":null},"transaction":
{"hash":"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b","confirmations":0}}}';

var_dump(json_decode($postedJsonData, true));

真正的参数会返回一个关联数组

【讨论】:

  • 数据正在发送到我的站点,尚未包含。
  • 您是否按照答案建议尝试了 print_r($_POST)。我应该向您展示正在发送给您的数据。
  • 我已经尝试过 var_dump 和 print_r 但是,由于在发送 POST 时我无法实际查看页面,所以我必须将结果发送到文本文件并且什么都没有传递。
【解决方案3】:

$data = json_decode($jsonData, True);

【讨论】:

  • 我可以将 assoc 设置为 TRUE,但我仍然需要在 $jsonData 变量中放置一个值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-14
  • 2018-12-29
  • 1970-01-01
  • 2016-09-09
  • 2015-09-18
  • 2014-05-28
相关资源
最近更新 更多