1

I have a JSON POST request being sent to my server.

It looks like this

{
"order": {
    "id": "5RTQNACF",
    "created_at": "2012-12-09T21:23:41-08:00",
    "status": "completed",
    "total_btc": {
        "cents": 100000000,
        "currency_iso": "BTC"
    },
    "total_native": {
        "cents": 1253,
        "currency_iso": "USD"
    },
    "custom": "order1234",
    "receive_address": "1NhwPYPgoPwr5hynRAsto5ZgEcw1LzM3My",
    "button": {
        "type": "buy_now",
        "name": "Alpaca Socks",
        "description": "The ultimate in lightweight footwear",
        "id": "5d37a3b61914d6d0ad15b5135d80c19f"
    },
    "transaction": {
        "id": "514f18b7a5ea3d630a00000f",
        "hash": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
        "confirmations": 0
    }
}
}

How can I retrieve the value of "cents" or any other variable?

4

4 回答 4

2

你需要json_decode()像这样解码它

$json = json_decode($jsonString);

这会给你一个对象,然后你可以像这样使用它

 echo $json->order->total_btc->cents;
于 2013-07-26T07:07:59.480 回答
0

尝试这个,

// let
$jsondata='{order:{...}}';
$json = json_decode($jsondata,true);
echo $json['order']['total_native']['cents'];
于 2013-07-26T07:10:56.367 回答
0

取决于您如何处理传入的数据。

正如 Dave Chen 所说,使用$_POST['order']['total_native']['cents'],但您需要先对它们进行解码,方法json_decode()是在特定的 $_POST 索引上使用。

然后,选择是否需要数组或对象:

$json_obj = json_decode($string);

将返回一个stdClass对象。并获取数据,使用这个$json_obj->total_btc->cents

但是,这:

$json_arr = json_decode($string, true);

将返回一个数组,您可以在其中获取值$json_arr['order']['total_native']['cents']

于 2013-07-26T07:12:38.497 回答
0

发送

$data_string = json_encode(array("customer"=>array("key"=>"val")));
$data_string = urlencode(json_encode($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, array("customer"=>$data_string));

接收它

$datastring = $_POST['customer'];
$data = json_decode( urldecode( $datastring));
于 2013-07-26T07:17:41.493 回答