[英]Receive JSON via POST?
I have a JSON POST request being sent to my server. 我有一个JSON POST请求发送到我的服务器。
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? 如何获取“分”或任何其他变量的值?
You need to decode it with json_decode()
like this 您需要像这样用
json_decode()
对其进行解码
$json = json_decode($jsonString);
That will give you an object and then you can use it like this 这将为您提供一个对象,然后您可以像这样使用它
echo $json->order->total_btc->cents;
Try this, 尝试这个,
// let
$jsondata='{order:{...}}';
$json = json_decode($jsondata,true);
echo $json['order']['total_native']['cents'];
Depending of how you treat the incoming datas. 取决于您如何处理传入数据。
As Dave Chen said, using $_POST['order']['total_native']['cents']
, but you need to decode them before, by using json_decode()
on the specific $_POST index. 正如Dave Chen所说,使用
$_POST['order']['total_native']['cents']
,但是您需要在特定的$ _POST索引上使用json_decode()
对其进行解码。
Then, choose if you need an array or an object : 然后,选择是否需要数组或对象:
$json_obj = json_decode($string);
Will return a stdClass
object. 将返回一个
stdClass
对象。 And to get datas, use this $json_obj->total_btc->cents
要获取数据,请使用此
$json_obj->total_btc->cents
However, this : 但是,这:
$json_arr = json_decode($string, true);
Will return an array, where you can get values with $json_arr['order']['total_native']['cents']
将返回一个数组,您可以在其中使用
$json_arr['order']['total_native']['cents']
获取值
sending 发出
$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 it 接受它
$datastring = $_POST['customer'];
$data = json_decode( urldecode( $datastring));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.