1

i am try to get the json output below so that i send it to an API

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

But somehow when the xml is inserted into the Json it ignores certain characters and i end up having the output below:

{   "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>",}

Below is the php code that encoding the JSON

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

Please help!

tendaitakas
  • 328
  • 5
  • 18

1 Answers1

1

First of all, I'm wondering why you are interfacing with an API in JSON and sending an XML payload? Can't you send the XML directly?

Regardless, you aren't using "json_encode" properly. You need to represent your data into an array or object.

Here's what you want to do

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

The result I'm getting seems like what you want :

{ 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>" }
  • Thank you very much. It turns out that doing output printout using var_dump instead of echo is the problem, so its now coming out. But it xml is coming out with extra back slashes, is there a way of removing these? – tendaitakas Mar 29 '17 at 10:28
  • 1
    The backslashes need to be there to escape the quotes and stay standard JSON – Simon Dupuis Howard Mar 30 '17 at 00:46