11

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?

3 Answers 3

7

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. It works flawlessly! I could not actually find out the "'Filter' => array" part.
0

I had similar problem and I had to post data in this structure. Accepted answer didn't work for me

$data = array(
  'GetResultListRequest' => array(
    'Filters' => array(
        array('Name' => 'string', 'Value' => 'string'),
        array('Name' => 'string', 'Value' => 'string'),
    )
  )
);

maybe it might help somebody if accepted answear doesn't work for you

Comments

-1

For example, you can try this:

$data1 = new SampleStruct();  
$data1->title="Hello world";  
$data1->description="This is a sample description.";

$data2 = new SampleStruct();
$data2->title="Hello world 2";
$data2->description="This is a sample description 2.";

$client->__soapCall("sampleFunction", array(
   new SoapParam(new SoapVar(array($data1, $data2) , SOAP_ENC_ARRAY, 
       "SampleStruct_Array", "http://www.w3.org/2001/XMLSchema"), 
       "theSampleFunctionParamName")
));

1 Comment

This does not answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.