duancui19840401 2016-05-13 17:43
浏览 25
已采纳

如何使用PHP在SOAP Response中包含多个相同类型的对象

I'm using PHP's SoapServer and I want the response to include multiple items of the same type. My wsdl for this section looks like this.

<xsd:element name="getSalesTaxResponse">
    <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="totalTaxAmount" type="xsd:string"></xsd:element>
            <xsd:element maxOccurs="unbounded" minOccurs="1" name="productTax" type="tns:getSalesTaxResultInformation" />
          </xsd:sequence>
    </xsd:complexType>
</xsd:element>
<xsd:complexType name="getSalesTaxResultInformation">
    <xsd:annotation>
        <xsd:documentation>This object stores information related to product tax request
        </xsd:documentation>
    </xsd:annotation>
    <xsd:sequence>
        <xsd:element name="productId" type="xsd:string"></xsd:element>
        <xsd:element name="productNRCPrice" type="xsd:string"></xsd:element>
        <xsd:element name="taxAmount" type="xsd:string"></xsd:element>
    </xsd:sequence>
</xsd:complexType>

In PHP my brain is stuck and I can't for the life of me figure out how to include multiple "productTax" records in the response. I'm currently doing this, but it doesn't do what I want.

        $this->response = new GetSalesTaxResponse();
        $this->response->totalTaxAmount = '21.93';
        $this->response->productTax = array(
            (object) array(
                'productId'=>'3123',
                'productNRCPrice'=>'201.20',
                'taxAmount'=>'10.10'),
            (object) array('productId'=>'2103',
                'productNRCPrice'=>'102.10',
                'taxAmount'=>'11.83')
        );
        file_put_contents('/tmp/burp', print_r($this->response, TRUE), FILE_APPEND);
        return $this->response->getSoapVar();

But in SOAP-UI, I see this.

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://www.example.com/SOAP/Billing">
   <SOAP-ENV:Body>
      <ns1:getSalesTaxResponse>
         <totalTaxAmount>21.93</totalTaxAmount>
         <productTax>
            <SOAP-ENC:Struct>
               <productId>3123</productId>
               <productNRCPrice>201.20</productNRCPrice>
               <taxAmount>10.10</taxAmount>
            </SOAP-ENC:Struct>
            <SOAP-ENC:Struct>
               <productId>2103</productId>
               <productNRCPrice>102.10</productNRCPrice>
               <taxAmount>11.83</taxAmount>
            </SOAP-ENC:Struct>
         </productTax>
      </ns1:getSalesTaxResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

That makes sense based on what I'm doing in PHP, but how do I build the response in PHP to look like this instead?

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://www.example.com/SOAP/Billing">
       <SOAP-ENV:Body>
          <ns1:getSalesTaxResponse>
             <totalTaxAmount>21.93</totalTaxAmount>
             <productTax>
                   <productId>3123</productId>
                   <productNRCPrice>201.20</productNRCPrice>
                   <taxAmount>10.10</taxAmount>
              </productTax>
              <productTax>
                   <productId>2103</productId>
                   <productNRCPrice>102.10</productNRCPrice>
                   <taxAmount>11.83</taxAmount>
             </productTax>
          </ns1:getSalesTaxResponse>
       </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
  • 写回答

1条回答 默认 最新

  • doutuobao9736 2016-05-14 21:51
    关注

    To set a complex type array from within PHP, you must create a new complex type which your getSalesTaxResponse uses, and has a sequence of elements with the array type of getSalesTaxResultInformation:

    <complexType name="getSalesTaxResultInformation_Array">
      <complexContent>
        <restriction base="SOAP-ENC:Array">
          <sequence>
            <element name="productTax" type="tns:getSalesTaxResultInformation"
                     maxOccurs="unbounded"/>
          </sequence>
        </restriction>
      </complexContent>
    </complexType>
    

    Which makes your getSalesTaxResponse as follows:

    <xsd:element name="getSalesTaxResponse">
        <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="totalTaxAmount" type="xsd:string"></xsd:element>
                <xsd:element maxOccurs="unbounded" minOccurs="1" name="productTax" type="tns:getSalesTaxResultInformation_Array" />
              </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
    

    This gives SoapServer enough information to work out what type your array data is, essentially a map

    If SoapServer doesn't find the right array element type, you'll have to cast each productTax row to getSalesTaxResultInformation yourself:

    $array_element = new SoapVar($response_array, SOAP_ENC_OBJECT, null, null, 'getSalesTaxResultInformation');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题