这可能对某些人来说很简单,但对我来说是一个挑战。从文档中,我在POST请求的主体中获取JSON对象。格式如下所示。
当我们没有给出json对象的变量名时,如何解码json对象。我以为我会得到一个变量名,这样我就可以这样解码$data = json_decode($jsonFromSource);
我想不出办法做同样的事,有人吗?感谢。
文档说明:POST请求将发送到企业提供的URL。将传递的以下POST参数如下 上面传递的参数将包含在POST请求正文中的JSON对象中。链接到简要文档https://app.kopokopo.com/push_api
{
"service_name" : "MPESA",
"business_number" : "888555",
"transaction_reference" : "DE45GK45"
"internal_transaction_id" : 3222
"transaction_timestamp" : "2013-03-18T13:57:00Z"
"transaction_type" : "Paybill"
"account_number" : "445534"
"sender_phone" : "+254903119111"
"first_name" : "John"
"middle_name" : "K"
"last_name" : "Doe"
"amount" : 4000
"currency" : "KES"
"signature" : "dfafwerewKkladaHOKJdafdf"
}
答案 0 :(得分:0)
我不确定你最初是如何检索数据的 - 也许这会有所帮助吗?
$url='http://www.example.com/api/feed.json';
$data=file_get_contents( $url );
$json=json_decode( $data,true );
foreach( $json as $param => $value ) echo $param, $value, '<br />';
答案 1 :(得分:0)
KOPOKOPO会将低于JSON的有效负载推送给您
"service_name=M-PESA&business_number=888555&transaction_reference=DE45GK45&internal_transaction_id=3222&transaction_timestamp=2013-03-18T13%3A57%3A00Z&transaction_type=Paybill&account_number=445=K.&last_name=Doe&amount=4000¤cy=KES"
然后您可以像这样编码:
$callbackJSONData = json_encode($_POST);
结果将是...
{"service_name":"M-PESA","business_number":"888555","transaction_reference":"DE45GK45","internal_transaction_id":"3222","transaction_timestamp":"2013-03-18T13:57:00Z","transaction_type":"Paybill","account_number":"445=K.","last_name":"Doe","amount":"4000","currency":"KES"}
$jdata = json_decode($post,true);
/*
* We get details from $_POST only
*/
$first_name = $jdata["first_name"];
$middle_name = $jdata["middle_name"];
$last_name = $jdata["last_name"];
$paid_amount = $jdata["amount"];
$paying_phone_number = str_replace('+254', '0', preg_replace('/[^0-9+]/', '', $jdata["sender_phone"]));
$transactionID = $jdata["internal_transaction_id"];
$transactionrefID = str_replace('-','',$jdata["service_name"]);
$MpesaCode = $jdata["transaction_reference"];
$providerCurrency = $jdata["currency"];
$providerSignature = $jdata["signature"];
$payment_date = date("Y-m-d H:i:s",strtotime($jdata["transaction_timestamp"]));
$payment_time_at = date("Y-M-d",strtotime(NOW)) .' at '.date("H:i A",strtotime(NOW));
$payment_type = $jdata['transaction_type'];
$payment_destination = $jdata['business_number'];