1

I have created a custom Magento2 Soap API to get data from a third party server and save it into Magento DB. Everything is working fine with data processing. Now When I print the result it is in an Object form but I need it in XML format only. Here is the code I am using to make the request:

 $wsdlurl = 'MagentoStore/soap?wsdl&services=CustomDataApiManagementV1';
 $token = 'XXXXXXXXXXXX';
    $opts = ['http' => ['header' => "Authorization: Bearer " . $token]];
    $context = stream_context_create($opts);
    $addArgs = array('xmldata'=> 'testData');

    try{
        $soapClient = new SoapClient($wsdlUrl);
        $soapClient->setSoapVersion(SOAP_1_2);
        $soapClient->setStreamContext($context);
        $soapResponse = $soapClient
                     ->CustomDataApiManagementV1ProcessData($addArgs);
        print_r($soapResponse);
    }catch(Exception $e){
        echo 'error';
    }

This print_r($soapResponse) is showing result as below:

stdClass Object ( [result] => success )

I need result in XML format only.

Please do let me know if anyone has already worked on it.

Manju Ch
  • 21
  • 2
  • Have you tried `wddx_serialize_value` function to convert your object in to xml? like this `echo wddx_serialize_value($soapResponse);` – Shahroze Nawaz Jul 27 '17 at 08:39
  • Thanks for your response, Shahroze! In this way, we can only change the data at the client site i.e. from where we are making a request to Magento SOAP API. BUt I to change this response at Magento API end, so the API requesting users won't need to do it at their end. – Manju Ch Jul 27 '17 at 09:44
  • @ManjuCh May i know how you solved this issue? – Gem Nov 15 '18 at 11:26
  • 1
    @Gem: It wasn't solved actually, We just modified the code at third party end to use it in Object form instead of XML. – Manju Ch Nov 16 '18 at 12:26

1 Answers1

0

Try the below code

$soapResponse = array_flip((array) $soapResponse);
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($soapResponse, array ($xml, 'addChild'));
print $xml->asXML();

Your will get result like below

<root><result>Success</result></root>

Source from How to convert array to SimpleXML

Vino
  • 101
  • 2
  • Hi, Thank you for your response. Actually, I want to change it at the server end, not at the client end. i.e. $soapResponse = $soapClient ->CustomDataApiManagementV1ProcessData($addArgs); $soapResponse should be in XML format. – Manju Ch Jul 28 '17 at 11:59
  • Is there any specific reason for your $soapResponse in xml format?. I am not sure Response will get xml format. – Vino Jul 28 '17 at 12:27