开发者

PHP SoapClient type mapping behaves differently

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?

Details:

A var_dump of the result from the web-service function looks like following:

  • if one item is in the result:

    array(3) { ["filterErg"]=> object(stdClass)#37 (1) { ["item"]=> object(stdClass)#38 (9) ...

  • if more than one item is in the result:

    array(3) { ["filterErg"]=> object(stdClass)#37 (1) { ["item"]=> array(16) ...

The name of the function is getFilter and the relevant parts of the WSDL File are:

<types>
  <schema ...>
    <complexType name="arrayFilter">
      <sequence>
        <el开发者_StackOverflow社区ement 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>


You can use SOAP_SINGLE_ELEMENT_ARRAYS option when you are creating SoapClient

$soapConfig = array(
    'soap_version' => SOAP_1_2,
    'features' => SOAP_SINGLE_ELEMENT_ARRAYS,
    'trace'    => true
);
$client = new SoapClient('http://localhost:8070/Services.wsdl', $soapConfig);


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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜