Putting xml as a value of a json object in php -
i try json output below send 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 xml inserted json ignores characters , end having 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 php code encoding 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!
first of all, i'm wondering why interfacing api in json , sending xml payload? can't send xml directly?
regardless, aren't using "json_encode" properly. need represent data array or object.
here's want 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 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>" }
Comments
Post a Comment