将 xml 作为 php 中 json 对象的值



我尝试获取下面的 JSON 输出,以便将其发送到 API

 { "payload": "<?xml version="1.0" encoding="UTF-8"?>rn<ns0:BulkMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"rnxmlns:ns0="http://www.econet.co.zw/BulkMessageSchema"rnxsi:schemaLocation="http://www.econet.co.zw/BulkMessageSchema BulkMessageSchema.xsd">rn<ns0:bulkType>SINGLE</ns0:bulkType>rn<ns0:singleMessage>rn<ns0:message>Sample single bulk message</ns0:message>rn<ns0:reference>hfhdjfdkljfjdlkfjkd</ns0:reference>rn<ns0:mobileNumbers>0773562319</ns0:mobileNumbers>rn<ns0:mobileNumbers>0774705932</ns0:mobileNumbers>rn<ns0:category>PROMOTIONAL</ns0:category>rn</ns0:singleMessage>rn</ns0:BulkMessage>", }

但是不知何故,当 xml 插入 JSON 时,它会忽略某些字符,我最终得到以下输出:

{   "payload": "SINGLE</ns0:bulkType>Sample single bulk message</ns0:message>hfhdjfdkljfjdlkfjkd</ns0:reference>0773562319</ns0:mobileNumbers>0774705932</ns0:mobileNumbers>PROMOTIONAL</ns0:category></ns0:singleMessage></ns0:BulkMessage>",}

下面是编码 JSON 的 php 代码

  $payload = '<?xml version="1.0" encoding="UTF-8"?>'.
'<ns0:BulkMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="http://www.econet.co.zw/BulkMessageSchema" xsi:schemaLocation="http://www.econet.co.zw/BulkMessageSchema BulkMessageSchema.xsd">'.
'<ns0:bulkType>SINGLE</ns0:bulkType>'.
'<ns0:singleMessage>'.
'<ns0:message>Sample single bulk message</ns0:message>'.
'<ns0:reference>'.$uid.'</ns0:reference>'.
'<ns0:mobileNumbers>0773562319</ns0:mobileNumbers>'.
'<ns0:mobileNumbers>0774705932</ns0:mobileNumbers>'.
'<ns0:category>PROMOTIONAL</ns0:category>'.
'</ns0:singleMessage>'.
'</ns0:BulkMessage>';
$jsonDataEncoded = json_encode($payload);

请帮忙!

首先,我想知道为什么要在 JSON 中与 API 接口并发送 XML 有效负载?您不能直接发送 XML 吗?

无论如何,您没有正确使用"json_encode"。您需要将数据表示为数组或对象。

这是你想做的

<?php
$payloadArr = array(
    'payload'=>
        '<?xml version="1.0" encoding="UTF-8"?>'.
        '<ns0:BulkMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="http://www.econet.co.zw/BulkMessageSchema" xsi:schemaLocation="http://www.econet.co.zw/BulkMessageSchema BulkMessageSchema.xsd">'.
        '<ns0:bulkType>SINGLE</ns0:bulkType>'.
        '<ns0:singleMessage>'.
        '<ns0:message>Sample single bulk message</ns0:message>'.
        '<ns0:reference>'.$uid.'</ns0:reference>'.
        '<ns0:mobileNumbers>0773562319</ns0:mobileNumbers>'.
        '<ns0:mobileNumbers>0774705932</ns0:mobileNumbers>'.
        '<ns0:category>PROMOTIONAL</ns0:category>'.
        '</ns0:singleMessage>'.
        '</ns0:BulkMessage>'
);
$payloadJson = json_encode($payloadArr);
header('Content-type: application/json;');
echo $payloadJson;

我得到的结果似乎是你想要的:

{ payload: "<?xml version="1.0" encoding="UTF-8"?><ns0:BulkMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="http://www.econet.co.zw/BulkMessageSchema" xsi:schemaLocation="http://www.econet.co.zw/BulkMessageSchema BulkMessageSchema.xsd"><ns0:bulkType>SINGLE</ns0:bulkType><ns0:singleMessage><ns0:message>Sample single bulk message</ns0:message><ns0:reference></ns0:reference><ns0:mobileNumbers>0773562319</ns0:mobileNumbers><ns0:mobileNumbers>0774705932</ns0:mobileNumbers><ns0:category>PROMOTIONAL</ns0:category></ns0:singleMessage></ns0:BulkMessage>" }

相关内容

  • 没有找到相关文章

最新更新