Reputation: 21
Am trying to arrange json data to be posted to database,ie Phonenumber from metadata.can someone help me please.thanks
{
"Body":{
"stkCallback":{
"MerchantRequestID":"26642-2152252-1",
"CheckoutRequestID":"ws_CO_DMZ_240346011_09022019093008828",
"ResultCode":0,
"ResultDesc":"The service request is processed successfully.",
"CallbackMetadata":{
"Item":[
{"Name":"Amount","Value":10.00},
{"Name":"MpesaReceiptNumber","Value":"NB92QAMYN2"},
{"Name":"Balance"},
{"Name":"TransactionDate","Value":20190209093032},
{"Name":"PhoneNumber","Value":254723513144}
]
}
}
}
}
my php code which i cant seem to be getting right on phonenumber
$PhoneNumber=$json['Body']['stkCallback']["CallbackMetadata"]['Item']['PhoneNumber'];
$sql = "UPDATE pay SET ResultCode='$ResultCode' where uniqs='$PhoneNumber'";
Upvotes: 0
Views: 68
Reputation: 3819
JSON
object into a PHP Object
first.Item
is an array, you'll need to loop over it to obtain your required field i.e. PhoneNumber
.So, following is an illustration of the same -
<?php
$jsonObj = '{
"Body":{
"stkCallback":{
"MerchantRequestID":"26642-2152252-1",
"CheckoutRequestID":"ws_CO_DMZ_240346011_09022019093008828",
"ResultCode":0,
"ResultDesc":"The service request is processed successfully.",
"CallbackMetadata":{
"Item":[
{"Name":"Amount","Value":10.00},
{"Name":"MpesaReceiptNumber","Value":"NB92QAMYN2"},
{"Name":"Balance"},
{"Name":"TransactionDate","Value":20190209093032},
{"Name":"PhoneNumber","Value":254723513144}
]
}
}
}
}';
// json_decode your json array here
$phpObj = json_decode($jsonObj, true);
// get items array from the main object
$items = $phpObj['Body']['stkCallback']['CallbackMetadata']['Item'];
// loop over items to get desired field
foreach($items as $obj) {
if($obj['Name'] === 'PhoneNumber')
$PhoneNumber = $obj['Value'];
}
echo $PhoneNumber;
?>
Upvotes: 0
Reputation: 33813
$data='{
"Body":{
"stkCallback":{
"MerchantRequestID":"26642-2152252-1",
"CheckoutRequestID":"ws_CO_DMZ_240346011_09022019093008828",
"ResultCode":0,
"ResultDesc":"The service request is processed successfully.",
"CallbackMetadata":{
"Item":[
{"Name":"Amount","Value":10.00},
{"Name":"MpesaReceiptNumber","Value":"NB92QAMYN2"},
{"Name":"Balance"},
{"Name":"TransactionDate","Value":20190209093032},
{"Name":"PhoneNumber","Value":254723513144}
]
}
}
}
}';
$number=false;
/* use `json_decode` to make the data usable */
$json=json_decode( $data );
/* find the array of items */
$items=$json->Body->stkCallback->CallbackMetadata->Item;
/* loop through the array until you find the one you want. */
foreach( $items as $obj ){
if( $obj->Name=='PhoneNumber' )$number=$obj->Value;
}
/* do something with the number... */
if( $number )echo $number;
In the question you embed the number in the sql - you should consider using a prepared statement
instead to avoid sql injection... like this perhaps
$number=false;
$json=json_decode( $data );
$items=$json->Body->stkCallback->CallbackMetadata->Item;
$code=$json->Body->stkCallback->ResultCode;
foreach( $items as $obj ){
if( $obj->Name=='PhoneNumber' )$number=$obj->Value;
}
if( $number ){
$sql='update `pay` set `resultcode`=? where `uniqs`=?';
$stmt=$db->prepare($sql);
$stmt->bind_param( 'is', $code, $number );
$stmt->execute();
}
Upvotes: 1
Reputation: 1013
That is because $json['Body']['stkCallback']["CallbackMetadata"]['Item']
is an array filled with objects ( with Name
and Value
).
Therefore you need to traverse it and fetch item whose Name === PhoneNumber
:
foreach ($json['Body']['stkCallback']["CallbackMetadata"]['Item'] as $item) {
if ($item['Name'] === 'PhoneNumber') {
$PhoneNumber = $item['Value'];
}
}
Let me know if it worked. Of course, before saving it to the DB you should check if $PhoneNumber
is actually filled.
Upvotes: 0