此文章摘自http://www.5do8.com/blog/doc/616/index.aspx
为我解决了大问题,特转载。
创建一个php文件:
$ws = "http://localhost:700/my.asmx?wsdl";
$client = new SoapClient($ws);
这两行执行完毕了就创建了SoapClent类了,然后我们可以看看该类的公开方法和数据类型:
echo("SOAP服务器提供的开放函数:");
//以下代码为我解决了问题,显示出传入参数的结构,并且更容易获得返回值的结构
//以下代码为我解决了问题,显示出传入参数的结构,并且更容易获得返回值的结构
echo('<pre>');
var_dump($client->__getFunctions());
echo('</pre>');
echo("SOAP服务器提供的Type:");
echo('<pre>');
var_dump($client->__getTypes());
echo('</pre>');
var_dump($client->__getFunctions());
echo('</pre>');
echo("SOAP服务器提供的Type:");
echo('<pre>');
var_dump($client->__getTypes());
echo('</pre>');
然后输出的值我们看看:
SOAP服务器提供的开放函数:
array(2) {
[0]=>
string(56) "GetGUIDNodeResponse GetGUIDNode(GetGUIDNode $parameters)"
[1]=>
string(56) "GetGUIDNodeResponse GetGUIDNode(GetGUIDNode $parameters)"
}
SOAP服务器提供的Type:
array(2) {
[0]=>
string(36) "struct GetGUIDNode {
string GUID;
}"
[1]=>
string(57) "struct GetGUIDNodeResponse {
string GetGUIDNodeResult;
}"
}
array(2) {
[0]=>
string(56) "GetGUIDNodeResponse GetGUIDNode(GetGUIDNode $parameters)"
[1]=>
string(56) "GetGUIDNodeResponse GetGUIDNode(GetGUIDNode $parameters)"
}
SOAP服务器提供的Type:
array(2) {
[0]=>
string(36) "struct GetGUIDNode {
string GUID;
}"
[1]=>
string(57) "struct GetGUIDNodeResponse {
string GetGUIDNodeResult;
}"
}
这样,需要执行的公开接口的信息一目了然,什么都很清楚了,然后执行方法即可。
echo("执行GetGUIDNode的结果:");
try {
$parameters=array('GUID'=>"5do8");
$username = $client->GetGUIDNode($parameters);
} catch (SoapFault $fault){
echo "Fault! code:",$fault->faultcode,", string: ",$fault->faultstring;
}
//echo(htmlspecialchars($client->__getLastRequest()));
echo('<pre>');
var_dump($username);
echo('</pre>');
try {
$parameters=array('GUID'=>"5do8");
$username = $client->GetGUIDNode($parameters);
} catch (SoapFault $fault){
echo "Fault! code:",$fault->faultcode,", string: ",$fault->faultstring;
}
//echo(htmlspecialchars($client->__getLastRequest()));
echo('<pre>');
var_dump($username);
echo('</pre>');
仔细看这些代码,执行的是$client->函数名称(参数)注意,这里的函数名称就是你需要执行的,参数一般是一个数组和类,数组的键或者类的名称就是服务器端的名称,一定要对应,才能发送过去!在这个例子中,服务器端所用到的是string GUID ,然后客户端用的是array('GUID'=>"5do8"),这样就可以了。
执行soap函数返回的知识STDclass,要查看具体的值就可以用var_dump或者print_r输出整个值,或者用foreach循环取道你想要的值:
foreach($username as $key => $val) {
echo($val);
}
echo($val);
}