Search code examples
phpsymfonysoapzend-soap

PHP Change Zendsoap response output


When i create a Zendsoap Server it works fine and i get the following output:

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://localhost:8080/soap" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <SOAP-ENV:Body>
  <ns1:getTeacherResponse>
     <return SOAP-ENC:arrayType="ns2:Map[1]" xsi:type="SOAP-ENC:Array">
        <item xsi:type="ns2:Map">
           <item>
              <key xsi:type="xsd:string">teacherID</key>
              <value xsi:type="xsd:string">011</value>
           </item>
           <item>
              <key xsi:type="xsd:string">name</key>
              <value xsi:type="xsd:string">Miss Piggy</value>
           </item>
        </item>
     </return>
  </ns1:getTeacherResponse>

it comes from the following array i put up to output:

array:1 [
  0 => array:2 [
    "teacherID" => "011"
    "name" => "Miss Piggy"
   ]
]

now i want to have an output like this:

...
<item>
<teacherID>011</teacherID>
<name>Miss Piggy</name>
</item>
...

how can i tell zendsoap how to format the response?


Solution

  • I found a way to solve this:

        $xml = new XMLWriter();
        $xml->openMemory();
    
        $xml->startElementNS(null, 'teacher', NULL);
    
        foreach ($arr as $key=>$value){
            $xml->startElementNS(null, $key, NULL);
            $xml->Text($value);
            $xml->endElement();
        }
        $xml->endElement();
    
    
        return new SoapVar($xml->outputMemory(), XSD_ANYXML);
    

    which turns to the correct xml

    ...
    <teacher>
        <teacherID>123</teacherID>
        <name>Miss Piggy</name>
    </teacher>
    ...