I have a nested multi dimensional json object and I want to make a post request with this json but I did not find a direct way to send post request with this json because I did not find a way to declare a json object in php and even i cant convert this json_object to array because of the posted data corrupted
json_object={ "messages": { "authentication": { "productToken": "your product token" }, "msg": [{ "body": { "type": "auto", "content": "Fallback Text for SMS" }, "to": [{ "number": "00316012345678" }], "from": "00316098765432", "allowedChannels": ["WhatsApp"], "richContent": { "conversation": [{ "text": "A text message with *bold* formatting in a speech bubble." }, { "text": "Another speech bubble" }, { "media": { "mediaName": "and an image", "mediaUri": "https://www.cm.com/cdn/web/nl-nl/blog/conversational-commerce.jpg", "mimeType": "image/jpeg" } }] } }] } }
i want to make a post request with this json_object but i cant declare a json object in php so i should Convert My json_object to Array but i could not convert this json_object to array without corrupting request data
this is my code
$data = http_build_query($json_object); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $resp = curl_exec($ch); if($e = curl_error($ch)) { echo $e; } else { $decoded=json_decode($resp); foreach($decoded as $key => $val) { echo $key . ':'. $val. '<br>'; } } curl_close($ch);
Can anyone help me sending this post request please ?
CodePudding user response:
Consider the following example.
<?PHP
$json_object = '{
"messages": {
"authentication": {
"productToken": "your product token"
},
"msg": [{
"body": {
"type": "auto",
"content": "Fallback Text for SMS"
},
"to": [{
"number": "00316012345678"
}],
"from": "00316098765432",
"allowedChannels": ["WhatsApp"],
"richContent": {
"conversation": [{
"text": "A text message with *bold* formatting in a speech bubble."
}, {
"text": "Another speech bubble"
}, {
"media": {
"mediaName": "and an image",
"mediaUri": "https://www.cm.com/cdn/web/nl-nl/blog/conversational-commerce.jpg",
"mimeType": "image/jpeg"
}
}]
}
}]
}
}';
$decoded = json_decode($json_object);
foreach($decoded as $key => $val) {
echo "$key: " . serialize($val) . '<br>';
}
?>
The result would be the following:
messages: O:8:"stdClass":2:{s:14:"authentication";O:8:"stdClass":1:{s:12:"productToken";s:18:"your product token";}s:3:"msg";a:1:{i:0;O:8:"stdClass":5:{s:4:"body";O:8:"stdClass":2:{s:4:"type";s:4:"auto";s:7:"content";s:21:"Fallback Text for SMS";}s:2:"to";a:1:{i:0;O:8:"stdClass":1:{s:6:"number";s:14:"00316012345678";}}s:4:"from";s:14:"00316098765432";s:15:"allowedChannels";a:1:{i:0;s:8:"WhatsApp";}s:11:"richContent";O:8:"stdClass":1:{s:12:"conversation";a:3:{i:0;O:8:"stdClass":1:{s:4:"text";s:57:"A text message with *bold* formatting in a speech bubble.";}i:1;O:8:"stdClass":1:{s:4:"text";s:21:"Another speech bubble";}i:2;O:8:"stdClass":1:{s:5:"media";O:8:"stdClass":3:{s:9:"mediaName";s:12:"and an image";s:8:"mediaUri";s:65:"https://www.cm.com/cdn/web/nl-nl/blog/conversational-commerce.jpg";s:8:"mimeType";s:10:"image/jpeg";}}}}}}}<br>
If there is a specific portion you want to address, you would need to target it from the current object.
If you want specific parts, you need to perform a lot of extra steps:
<?php
$json_object = '{
"messages": {
"authentication": {
"productToken": "your product token"
},
"msg": [{
"body": {
"type": "auto",
"content": "Fallback Text for SMS"
},
"to": [{
"number": "00316012345678"
}],
"from": "00316098765432",
"allowedChannels": ["WhatsApp"],
"richContent": {
"conversation": [{
"text": "A text message with *bold* formatting in a speech bubble."
}, {
"text": "Another speech bubble"
}, {
"media": {
"mediaName": "and an image",
"mediaUri": "https://www.cm.com/cdn/web/nl-nl/blog/conversational-commerce.jpg",
"mimeType": "image/jpeg"
}
}]
}
}]
}
}';
$decoded = json_decode($json_object);
foreach($decoded->messages->msg as $message){
echo "To: " . $message->to[0]->number . "<br>\r\n";
echo "From: " . $message->from . "<br>\r\n";
foreach($message->richContent->conversation as $val){
if(isset($val->text)){
$txt = preg_replace("#\*([^*] )\*#", "<b>$1</b>", $val->text);
echo $txt . "<br>\r\n";
} elseif(isset($val->media)) {
if($val->media->mimeType == "image/jpeg"){
echo "<img src='{$val->media->mediaUri}' title='{$val->media->mediaName}'>\r\n";
}
}
}
};
This results in:
To: 00316012345678<br>
From: 00316098765432<br>
A text message with <b>bold</b> formatting in a speech bubble.<br>
Another speech bubble<br>
<img src="https://www.cm.com/cdn/web/nl-nl/blog/conversational-commerce.jpg" title="and an image">