2013-07-26 131 views
1

我有一個JSON POST請求被髮送到我的服務器。通過POST接收JSON?

它看起來像這樣

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

我怎樣才能檢索到的「仙」或任何其他變量的值?

+1

'$ _POST ['order'] ['total_native'] ['cents']'? –

+1

也許看看'json_decode(,true)' –

+0

使用json_decode($ response) - 你將擁有所有的數組形式。 –

回答

2

你需要將它與json_decode()這樣

$json = json_decode($jsonString); 

這會給你一個對象進行解碼,然後你可以使用它像這樣

echo $json->order->total_btc->cents; 
0

試試這個,

// let 
$jsondata='{order:{...}}'; 
$json = json_decode($jsondata,true); 
echo $json['order']['total_native']['cents']; 
+0

json_decode返回一個對象,除非你傳遞'true'作爲第二個參數 – Ahmad

+0

是的,我的錯誤,'更新了'我的'答案'。 –

0

取決於您如何處理傳入數據。

正如Dave Chen所說,使用$_POST['order']['total_native']['cents'],但您需要先解碼它們,在特定$ _POST索引上使用json_decode()

然後,選擇,如果你需要一個數組或對象:

$json_obj = json_decode($string); 

將返回stdClass對象。並獲得DATAS,使用此$json_obj->total_btc->cents

然而,這樣的:

$json_arr = json_decode($string, true); 

會返回一個數組,在那裏你可以$json_arr['order']['total_native']['cents']

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

獲取值recive它

$datastring = $_POST['customer']; 
$data = json_decode(urldecode($datastring));