11

问候,

我似乎找不到一种方法来创建以数组为参数的函数请求。例如,我如何使用 PHP SoapClient 发出这种请求:

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

是否可以在不创建任何额外类的情况下调用此函数(仅使用数组)?如果不是,那么最紧凑的调用方式是什么?

4

3 回答 3

7

您可以使用此-v函数将数组转换为对象树:

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

像这样:

$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);
于 2009-02-24T00:03:33.197 回答
0

我有类似的问题,我不得不以这种结构发布数据。 接受的答案对我不起作用

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

如果接受的 answear 对您不起作用,也许它可能会对某人有所帮助

于 2020-06-03T18:31:17.013 回答
-1

例如,你可以试试这个:

$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")
));
于 2012-05-16T05:59:36.303 回答