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

如何使用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');
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报