简体   繁体   English

将数组传递给 PHP 中的 SOAP 函数

[英]Passing array to SOAP function in PHP

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:例如,我如何使用 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?如果不是,最简洁的调用方式是什么?

You can use this -v function to convert a array to a object tree:您可以使用此-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;
}

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

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如果接受 answear 对你不起作用,也许它可能会帮助某人

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM