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?
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);
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
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")
));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.