PHP更改Zendsoap响应输出

时间:2017-07-20 14:36:36

标签: php symfony soap zend-soap

当我创建Zendsoap服务器时,它工作正常,我得到以下输出:

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

它来自我提出输出的以下数组:

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

现在我希望得到这样的输出:

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

我如何告诉zendsoap如何格式化响应?

1 个答案:

答案 0 :(得分:0)

我找到了解决这个问题的方法:

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

转向正确的xml

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