我用 PHP Soapserver 在 php 中创建了一个肥皂服务器。我创建了一个函数“addItems”,将多个项目作为数组添加到第一个参数和相应的 wsdl 文档中。在 wsdl 中,我创建了一个数组对象“项目”,如下所示:
<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 处理数组的方式。当我只添加一个项目时,项目列表不会作为数组返回。当我添加两个项目时,项目数组存储在一个项目元素中,而不是预期的项目参数中。
我尝试使用 soapenc:array,但这似乎与http://www.ws-i.org/Profiles/BasicProfile-1.0-2004-04-16.html#refinement16556272中提到的 WS-I 合规性不兼容,而且它也失败了当我想要在使用这种类型的编码时将服务添加到 Visual Studio 时。
那么我做错了什么?我想我必须在某个地方更改 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>