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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面