PHP可以使用WSDL文件生成一组适当的方法,您可以将泛型对象、数组或标量作为参数传递给这些方法。还可以使用SoapClient类的第二个参数指定哪些类映射到哪些方法(classmap选项),以及哪些类型声明映射到哪些序列化回调函数(typemap选项)。在class doRequestMethod {
public $id;
public $attribute;
}
class theResponseClass {
/* ... */
}
$options = array(
'classmap' => array(
'doRequest' => 'doRequestMethod',
'theResponse' => 'theResponseClass'
/* ... */
),
'typemap' => array(
0 => array(
'type_ns' => 'http://example.com/schema/wsdl_type.xsd',
'type_name"' => 'wsdl_type',
'from_xml' => function ($xml_string) { /* ... */ },
'to_xml' => function ($soap_object) { /* ... */ }
)
/* ... */
)
)
$client = new SoapClient('/path/to/filename.wsdl', $options);
$request = new doRequestMethod();
$request->id = 0;
$request->attribute = "FooBar";
$result = $client->doRequest($request);
/*
* If 'dorequest' returns a 'theResponse' in the WSDL,
* then $result should of the type 'theResponseClass'.
*/
assert(get_class($result) === 'theResponseClass');
这需要大量的工作,所以我建议将SoapClient子类化以供您自己使用。此外,为了使代码更易于调试,请尽可能经常使用PHP类型提示函数和参数参数。它可以防止一整类错误,而且值得对性能造成较小的损失。在