fwho
fwho

Reputation: 248

How do I turn a POSTed JSON value into PHP?

As a test, this JSON data is being POSTed to my website:

{
    "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
        }
    }
}

Since it is being sent server-to-server I cannot visibly see the data. I have tried sending the $_POST array to a text file, but it comes up blank. What I think I need to do is:

$data = json_decode($jsonData);

But how do I set the variable $jsonData?

Upvotes: 0

Views: 297

Answers (3)

zoonman
zoonman

Reputation: 1163

You can try to use wrappers for reading raw POST query.

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

Upvotes: 3

gaurav
gaurav

Reputation: 91

$data = json_decode($jsonData, True);

Upvotes: 0

luckystars
luckystars

Reputation: 1754

Have you tried this, store the sting obtained to a variable and then decoding it?

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

The true parameter would return an associative array

Upvotes: 0

Related Questions