PHP SoapClient 类型映射的行为不同
我有一个 Web 服务函数,它向 PHP 客户端返回一组项目.根据项目的数量,PHP 返回类型不同.如果函数返回一项,则 PHP 类型为 stdClass
如果函数返回多项,则 PHP 类型为 array
.无论哪种情况,它都应该是 array
.我可以做些什么来实现这一目标?
I've a web-service function which is returning an array of items to a PHP-Client. Depending on the number of items, the PHP return type is differently. If the function returns one item the PHP type is stdClass
if the function returns more than one item, the PHP type is array
. In either case it should be array
. What can I do to achieve this?
详细信息:
Web 服务函数结果的var_dump
如下所示:
Details:
Avar_dump
of the result from the web-service function looks like following:
- 如果结果中有一项:
array(3) { ["filterErg"]=>object(stdClass)#37 (1) { ["item"]=>object(stdClass)#38 (9) ...
- 如果结果中有多个项目:
array(3) { ["filterErg"]=>object(stdClass)#37 (1) { ["item"]=>数组(16) ...
函数名称为getFilter
,WSDL文件的相关部分为:
The name of the function is getFilter
and the relevant parts of the WSDL File are:
<types>
<schema ...>
<complexType name="arrayFilter">
<sequence>
<element name="item" type="ns1:stFilter" minOccurs="0" maxOccurs="unbounded" nillable="true"/>
</sequence>
</complexType>
...
</schema>
</types>
<message name="getFilterResponse">
<part name="filterErg" type="ns1:arrayFilter"/>
<part name="functionResult" type="xsd:int"/>
<part name="outErr" type="xsd:string"/>
</message>
<portType name="ADServicePortType">
<operation name="getFilter">
<documentation>Service definition of function ns1__getFilter</documentation>
<input message="tns:getFilter"/>
<output message="tns:getFilterResponse"/>
</operation>
...
</portType>
<binding name="ADService" type="tns:ADServicePortType">
<SOAP:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="getFilter">
<SOAP:operation style="rpc" soapAction=""/>
<input>
<SOAP:body use="encoded" namespace="urn:ADService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<SOAP:body use="encoded" namespace="urn:ADService" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
...
</binding>
有时将变量从对象更改为包含该对象的数组:
Change the variable from object to an array containing the object on occasion:
if (is_object($variable))
{
$variable = array($variable);
}
或者更具体地说是您的情况:
Or more specifically in your case:
if (is_object($result["filterErg"]->item))
{
$result["filterErg"]->item = array($result["filterErg"]->item);
}