我有一个Web服务函数,它将一个项目数组返回给PHP-Client。根据项目的数量,PHP返回类型是不同的。如果函数返回一个项,则PHP类型为stdClass
如果函数返回多个项,则PHP类型为array
。无论哪种情况,都应该是array
。我能做些什么来实现这个目标?
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>
答案 0 :(得分:7)
创建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);
答案 1 :(得分:2)
有时将变量从object更改为包含该对象的数组:
if (is_object($variable))
{
$variable = array($variable);
}
或者更具体地说是你的情况:
if (is_object($result["filterErg"]->item))
{
$result["filterErg"]->item = array($result["filterErg"]->item);
}