我正在使用PHP的SoapServer,我希望响应包含多个相同类型的项目。我对此部分的看法是这样的。
<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>
在PHP中,我的大脑陷入困境,我不能为我的生活弄清楚如何在响应中包含多个“productTax”记录。我现在正在这样做,但它没有做我想做的事。
$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();
但在SOAP-UI中,我看到了这一点。
<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>
根据我在PHP中所做的事情,这是有道理的,但我如何在PHP中构建响应看起来像这样呢?
<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>
答案 0 :(得分:1)
要在PHP中设置复杂类型数组,您必须创建getSalesTaxResponse
使用的新复杂类型,并且具有数组类型为getSalesTaxResultInformation
的元素序列:
<complexType name="getSalesTaxResultInformation_Array">
<complexContent>
<restriction base="SOAP-ENC:Array">
<sequence>
<element name="productTax" type="tns:getSalesTaxResultInformation"
maxOccurs="unbounded"/>
</sequence>
</restriction>
</complexContent>
</complexType>
这使您的getSalesTaxResponse
如下:
<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>
这为SoapServer提供了足够的信息来确定您的数组数据的类型,实质上是一个地图
如果SoapServer找不到合适的数组元素类型,则必须自己将每个productTax
行转换为getSalesTaxResultInformation
:
$array_element = new SoapVar($response_array, SOAP_ENC_OBJECT, null, null, 'getSalesTaxResultInformation');