我有一个web服务函数,它将向PHP客户端返回一组项。根据项目的数量,PHP返回类型有所不同。如果函数返回一个项,则PHP类型为stdClass
;如果函数返回多个项,那么PHP类型为array
。在任何一种情况下,它都应该是array
。我能做些什么来实现这一点?
详细信息:
web服务功能的结果的var_dump
如下所示:
- 如果结果中有一项:
array(3) { ["filterErg"]=> object(stdClass)#37 (1) { ["item"]=> object(stdClass)#38 (9) ...
- 如果结果中有多个项目:
array(3) { ["filterErg"]=> object(stdClass)#37 (1) { ["item"]=> array(16) ...
函数的名称为getFilter
,WSDL文件的相关部分为:
<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>
创建SoapClient 时,可以使用SOAP_SINGLE_ELEMENT_ARRAYS选项
$soapConfig = array(
'soap_version' => SOAP_1_2,
'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
'trace' => true
);
$client = new SoapClient('http://localhost:8070/Services.wsdl', $soapConfig);
有时将变量从对象更改为包含对象的数组:
if (is_object($variable))
{
$variable = array($variable);
}
或者更具体地说,在您的情况下:
if (is_object($result["filterErg"]->item))
{
$result["filterErg"]->item = array($result["filterErg"]->item);
}