我用php Soapserver在php中创建了一个soap服务器。我创建了一个函数'addItems',在第一个参数和相应的wsdl文档中添加多个项目作为数组。在wsdl中,我创建了一个数组对象'items',如下所示:
<s:complexType name="items">
<s:sequence>
<s:element nillable="true" name="item" type="tns:item" minOccurs="0" maxOccurs="unbounded"/>
</s:sequence>
</s:complexType>
php函数只是一个传递给php soapserver的简单类方法,如下所示:
<?php
class Test
{
public function addItems($items) {
print_r($items);
}
}
$service = new Test();
$server = new SoapServer('wsdl.xml');
$server->setObject();
print($server->handle());
问题就像php soapserver处理数组一样。当我只添加一个项目时,项目列表不会作为数组返回。当我添加两个项目时,项目数组存储在item元素而不是期望的items参数中。
我尝试使用soapenc:array,但这似乎与http://www.ws-i.org/Profiles/BasicProfile-1.0-2004-04-16.html#refinement16556272中提到的WS-I合规性不兼容,而且当我想在使用这种类型的编码时将服务添加到visual studio时,它也会失败。 / p>
那么我做错了什么?我想我必须在某个地方改变wsdl,但找不到一个有效的例子。或者我是否必须为soapserver设置一些选项?我输了。
下面是请求,实际结果和预期结果:
请求一项:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:addItems" xmlns:wsdl="http://base.shopzonline.com/soap/api/test/?wsdl">
<soapenv:Header/>
<soapenv:Body>
<urn:addItems>
<items>
<!--Zero or more repetitions:-->
<wsdl:item>
<wsdl:id>1</wsdl:id>
<wsdl:name>a</wsdl:name>
</wsdl:item>
</items>
</urn:addItems>
</soapenv:Body>
</soapenv:Envelope>
$ items参数的实际结果
Array
(
[item] => Array
(
[id] => 1
[name] => a
)
)
$ items参数的预期结果
Array
[0] => (
Array
(
[id] => 1
[name] => a
)
)
请求多个:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:addItems" xmlns:wsdl="http://base.shopzonline.com/soap/api/test/?wsdl">
<soapenv:Header/>
<soapenv:Body>
<urn:addItems>
<items>
<!--Zero or more repetitions:-->
<wsdl:item>
<wsdl:id>1</wsdl:id>
<wsdl:name>a</wsdl:name>
</wsdl:item>
<wsdl:item>
<wsdl:id>2</wsdl:id>
<wsdl:name>b</wsdl:name>
</wsdl:item>
</items>
</urn:addItems>
</soapenv:Body>
</soapenv:Envelope>
$ items参数的实际结果
Array
(
[item] => Array
(
[0] => Array
(
[id] => 1
[name] => a
)
[1] => Array
(
[id] => 2
[name] => b
)
)
)
预期结果
Array
(
[0] => Array
(
[id] => 1
[name] => a
)
[1] => Array
(
[id] => 2
[name] => b
)
)
完整的WSDL下面:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions name="LocalhostTestWebservice"
targetNamespace="http://localhost/soap/api/test/?wsdl"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:tns="http://localhost/soap/api/test/?wsdl"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:s="http://www.w3.org/2001/XMLSchema"
>
<wsdl:types>
<s:schema elementFormDefault="qualified" attributeFormDefault="unqualified" targetNamespace="http://localhost/soap/api/test/?wsdl">
<s:complexType name="addItemsResponseElement">
<s:sequence>
<s:element name="result" minOccurs="0" type="s:int"/>
</s:sequence>
</s:complexType>
<s:element name="addItemsResponseElement" nillable="true" type="tns:addItemsResponseElement" />
<s:complexType name="item">
<s:sequence>
<s:element name="id" type="s:string" />
<s:element name="name" type="s:string" />
</s:sequence>
</s:complexType>
<s:element name="item" type="tns:item" />
<s:complexType name="items">
<s:sequence>
<s:element nillable="true" name="item" type="tns:item" minOccurs="0" maxOccurs="unbounded"/>
</s:sequence>
</s:complexType>
<s:element name="items" type="tns:items" />
</s:schema>
</wsdl:types>
<wsdl:message name="addItemsRequest">
<wsdl:part name="items" type="tns:items" />
</wsdl:message>
<wsdl:message name="addItemsResponse">
<wsdl:part name="parameters" type="tns:addItemsResponseElement"/>
</wsdl:message>
<wsdl:portType name="TestPortType">
<wsdl:operation name="addItems">
<wsdl:input message="tns:addItemsRequest" />
<wsdl:output message="tns:addItemsResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="LocalhostTestWebserviceBinding" type="tns:TestPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="addItems">
<soap:operation soapAction="addItems" />
<wsdl:input>
<soap:body use="literal" namespace="urn:addItems" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" namespace="urn:addItems" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="LocalhostTestWebservice">
<wsdl:port name="LocalhostTestWebservicePort" binding="tns:LocalhostTestWebserviceBinding">
<soap:address location="http://localhost/api/soap/test/" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
答案 0 :(得分:1)
您可以通过在SoapServer上启用SOAP_SINGLE_ELEMENT_ARRAYS
来解决此问题:
$server = new SoapServer('wsdl.xml', [
'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
]);
答案 1 :(得分:0)
我没有测试此解决方案,但您可以查看:
在消息部分中:
<message name="getItemsResponse">
<part name="items" type="ns:ArrayOfItems"/>
</message>
类型部分:
<xsd:schema targetNamespace="http://foo.bar/objectsoapserver/types"
xmlns="http://foo.bar/objectsoapserver/types">
<xsd:complexType name="ArrayOfItems">
<xsd:complexContent>
<xsd:restriction base="soapenc:Array">
<xsd:attribute ref="soapenc:arrayType" soap:arrayType="ns:Items[]"/>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
<xsd:complexType name="Items">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="id" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="Items" nillable="true" type="ns:Items"/>
</xsd:schema>
顺便说一句。为什么不使用库自动生成WSDL?