Passing array to SOAP function in PHP

给你一囗甜甜゛ 提交于 2020-01-19 12:43:06

问题


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?


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/577795/passing-array-to-soap-function-in-php

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