PHP SoapClient 类型映射的行为不同 [英] PHP SoapClient type mapping behaves differently

查看:29
本文介绍了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:

A var_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);
}

这篇关于PHP SoapClient 类型映射的行为不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆