[英]Passing array to SOAP function in PHP
你好,
我似乎找不到一種方法來創建一個以數組為參數的函數請求。 例如,我如何使用 PHP SoapClient 發出這種請求:
<GetResultList>
<GetResultListRequest>
<Filters>
<Filter>
<Name>string</Name>
<Value>string</Value>
</Filter>
<Filter>
<Name>string</Name>
<Value>string</Value>
</Filter>
</Filters>
</GetResultListRequest>
</GetResultList>
是否可以在不創建任何額外類的情況下調用此函數(僅使用數組)? 如果不是,最簡潔的調用方式是什么?
您可以使用此-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);
我有類似的問題,我不得不在這個結構中發布數據。 接受的答案對我不起作用
$data = array(
'GetResultListRequest' => array(
'Filters' => array(
array('Name' => 'string', 'Value' => 'string'),
array('Name' => 'string', 'Value' => 'string'),
)
)
);
如果接受 answear 對你不起作用,也許它可能會幫助某人
例如,你可以試試這個:
$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.