Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing array to SOAP function in PHP

Tags:

php

soap

wsdl

Greetings,

I can't seem to find a way to create a function request with array as an argument. For example, how do I make this kind of request using PHP SoapClient:

<GetResultList>
  <GetResultListRequest>
    <Filters>
      <Filter>
        <Name>string</Name>
        <Value>string</Value>
      </Filter>
      <Filter>
        <Name>string</Name>
        <Value>string</Value>
      </Filter>
    </Filters>
  </GetResultListRequest>
</GetResultList>

Is this possible to call this function without creating any extra classes (using arrays only)? If no, what is the most compact way of calling it?

like image 321
bezmax Avatar asked Feb 23 '09 14:02

bezmax


1 Answers

You can use this -v function to convert a array to a object tree:

function array_to_objecttree($array) {
  if (is_numeric(key($array))) { // Because Filters->Filter should be an array
    foreach ($array as $key => $value) {
      $array[$key] = array_to_objecttree($value);
    }
    return $array;
  }
  $Object = new stdClass;
  foreach ($array as $key => $value) {
    if (is_array($value)) {
      $Object->$key = array_to_objecttree($value);
    }  else {
      $Object->$key = $value;
    }
  }
  return $Object;
}

Like so:

$data = array(
  'GetResultListRequest' => array(
    'Filters' => array(
      'Filter' => array(
        array('Name' => 'string', 'Value' => 'string'), // Has a numeric key
        array('Name' => 'string', 'Value' => 'string'),
      )
    )
  )
);
$Request = array_to_objecttree($data);
like image 134
Bob Fanger Avatar answered Nov 15 '22 12:11

Bob Fanger