0

PHP seems to treat one element arrays as objects when returned from an ASMX web service. This can be problematic for example when using the count() function, where the supplied argument may not end up being an array at all. Since PHP 8, count() (or array_search()) throws an error instead of a warning if the supplied argument is not an array,.

sample wsdl response definition:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ws_get_audit_detailsResponse xmlns="http://tempurl.org">
      <ws_get_audit_detailsResult>
        <ws_status>int</ws_status>
        <ws_message>string</ws_message>
        <audit_dets>
          <audit_details>
            <audit_date>dateTime</audit_date>
            //some other data
          </audit_details>
          <audit_details>
            <audit_date>dateTime</audit_date>
            //some other data
          </audit_details>
        </audit_dets>
      </ws_get_audit_detailsResult>
    </ws_get_audit_detailsResponse>
  </soap:Body>
</soap:Envelope>

When using the below code to consume an asmx web service:

$wsdl = "www.some_url.com/app/webservice.asmx?wsdl";
$client = new SoapClient ($wsdl, array('cache_wsdl' => WSDL_CACHE_NONE) );
$result = $client->{$function_name}($parameters)->{$web_service."Result"};

If $result->audit_dets->audit_details contains one element, it is treated as a stdClass as opposed to an array of stdClass. Meaning i need to write if statements using 'is_array()' to handle both scenarios.

In C#, this problem doesnt occur when creating a service reference. So why does PHP behave like this and not return an array with one element in it?

Notaras
  • 611
  • 1
  • 14
  • 44
  • This is probably more to do with the implementation of the interface than PHP. (AFAIK) There is nothing in PHP that would automatically covert a single element array to an object, but when creating an API, it's easy to do this as some perceive it as a better solution. – Nigel Ren Dec 02 '21 at 07:16
  • @NigelRen as another user pointed out, i needed to pass `SOAP_SINGLE_ELEMENT_ARRAYS ` to the soapclient options – Notaras Dec 02 '21 at 23:08

0 Answers0