PHP SoapClient type mapping behaves differently

≯℡__Kan透↙ 提交于 2019-12-10 16:46:00

问题


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>
        <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>

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/6408283/php-soapclient-type-mapping-behaves-differently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!