无法在 PHP 中发送嵌套的多维 json 对象 POST 请求,因为多维 json 在将其转换为数组时已损坏

Could not send nested multi dimensional json object POST request in PHP because multi dimensional json corrupted when converting it to array

我有一个嵌套的多维 json 对象,我想用这个 json 发出 post 请求 但是我没有找到用这个 json 发送 post 请求的直接方法,因为我没有找到在 php 中声明 json 对象的方法,甚至我也无法转换这个 json_object 到数组因为 posted 数据损坏

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"
                    }
                }]
            }
        }]
    }
}

我想用这个 json_object 发出一个 post 请求,但我不能在 php 中声明一个 json 对象所以我应该将我的 json_object 转换为数组,但我无法在不破坏请求数据的情况下将此 json_object 转换为数组

这是我的代码

$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);

谁能帮我发送这个 post 请求?

考虑以下示例。

<?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>';
}
?>

结果如下:

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>

如果您想要解决某个特定部分,则需要从当前对象中定位它。

如果你想要特定的部分,你需要执行很多额外的步骤:

<?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></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";
            }
        }
    }
};

这导致:

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">