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 ?