2013-07-26 88 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));