demo如下:
一.客户端soapclient.php:
http://localhost/webservice/soapservice.php,
'uri' => 'soapservice.php' ) );
//调用函数
$result1 = $soap->addition ( 200, 160 );
echo $result1;
}
catch ( soapfault $e ) { echo $e->getmessage (); }
catch ( exception $e ) { echo $e->getmessage (); }
?>
二.服务端soapservice.php:
soapservice.php));
$server -> setclass(calculator);
$server -> handle();
class calculator
{
function addition($num1,$num2) {
$result = $num1+$num2;
return {$num1} 加 {$num2} ,结果为 .$result. !;
}
}
?>
如果服务端是用php写的。这样看得懂,这个demo也是正确的。
可如果服务端不是知道是用什么语言写的。我用soapclient要怎样通信呢?
第三方提供了一个请求的url:
http://www.xxx.com/ipcam/soapservice
回复讨论(解决方案) 需要wsdl
客户端如下:
$soap = new soapclient(http://www.xxx.com/ipcam/soapservice/math.wsdl);
$result1 = $soap->addition ( 200, 160 );
echo $result1;
一样的,只要对方有 addition 方法,且参数格式也符合就行
需要wsdl
客户端如下:
$soap = new soapclient(http://www.xxx.com/ipcam/soapservice/math.wsdl);
$result1 = $soap->addition ( 200, 160 );
echo $result1;
第三方不提供wsdl。所以用soapclient能用吗?
一样的,只要对方有 addition 方法,且参数格式也符合就行
除了php的soapclient方法通信外是不是可以直接用http请求来完成通信?
注:第三方提供了soap消息的结构。第三方反馈的信息也是soap信息结构。
http请求时加上soap的消息结构
xmlns:soap=http://www.w3.org/2001/12/soap-envelope
soap:encodingstyle=http://www.w3.org/2001/12/soap-encoding>
...
...
...
...
...
...
你不是说有 .net 的示例吗?贴出来看看
public static string client_version = 1.0.0; public const string def_soapaddr_withdigestauth = http://www.xxx.com/ipcam/soapservice; public const string def_soapaddr_withsessionidauth = http://www.xxx.com/ipcam/ssservice; public const string def_soapaddr_action = http://www.xxx.com/userservice/useroperation; /* user registration */ public static int soapuserregistration( string equipno, string authcode, string version, string username, string password, string confirmpassword, string emailbox, string oemid, string devicename, string deviceattr, stringbuilder bufretsoapconfigdata, int maxretsoapconfigdatasize, ref soaperror_info infosoaperror) { int retvalue = -1; string strresponse = ; /* check input parameters */ if (oemid == ) { oemid = 0; } if (version == ) { version = client_version; } string strrandom = getrandomstr32(); //构造soap请求信息 stringbuilder soap = new stringbuilder(); soap.append(); soap.append(\n); soap.append(); soap.append(); soap.append(); soap.append( + version + ); soap.append(gbk); soap.append(userregistration); soap.append( + strrandom + ); soap.append( + devicename + ); soap.append( + deviceattr + ); soap.append(); soap.append( + oemid + ); soap.append( + username + ); soap.append( + password + ); soap.append( + confirmpassword + ); soap.append( + emailbox + ); soap.append(); soap.append(); soap.append(); soap.append(); byte[] parambytes = encoding.utf8.getbytes(soap.tostring()); for (int j = 0; j < 2; j++) { try { //发起请求 uri uri = new uri(def_soapaddr_withdigestauth); httpwebrequest webrequest = (httpwebrequest)httpwebrequest.create(uri); //string strpasswordmd5 = md5(authcode); webrequest.preauthenticate = true; networkcredential mycred = new networkcredential(equipno, authcode); webrequest.credentials = mycred; webrequest.method = post; webrequest.useragent = fsoap/1.0; webrequest.contenttype = text/xml; charset=utf-8; webrequest.contentlength = parambytes.length; //webrequest.expect = ; webrequest.keepalive = false; webrequest.headers.add(soapaction, def_soapaddr_action); webrequest.timeout = max_remote_service_timeout; webrequest.servicepoint.expect100continue = false; using (stream requeststream = webrequest.getrequeststream()) { requeststream.write(parambytes, 0, parambytes.length); } webresponse webresponse = webrequest.getresponse(); stream streamresponse = webresponse.getresponsestream(); using (streamreader mystreamreader = new streamreader(streamresponse, encoding.utf8)) { strresponse = mystreamreader.readtoend(); // close the stream object. mystreamreader.close(); streamresponse.close(); // release the httpwebresponse. webresponse.close(); if (strresponse != ) { xmldocument xml = new xmldocument(); xml.loadxml(strresponse); xmlnode rootnode = xml.selectsinglenode(//wsresponse); if (rootnode != null) { xmlnode node_errorinfo = null; xmlnode node_code = null; xmlnode node_description = null; /* errorinfo */ node_errorinfo = rootnode.selectsinglenode(errorinfo); if (node_errorinfo != null) { node_code = node_errorinfo.selectsinglenode(code); if (node_code != null) { infosoaperror.bufcode = node_code.innertext; if (infosoaperror.bufcode == 0) { retvalue = 0; } else { retvalue = -2; } } node_description = node_errorinfo.selectsinglenode(description); if (node_description != null) { infosoaperror.bufdescription = node_description.innertext; } } } } break; } } catch (system.net.webexception webex) { if (webex.response != null) { if (webex.status == webexceptionstatus.timeout) { continue; } else { httpstatuscode status = ((httpwebresponse)webex.response).statuscode; int errvalue = ((int)status); retvalue = errvalue; continue; } } else { retvalue = -1; continue; } } catch { retvalue = -1; continue; } } return retvalue; }
还有一些变量的定义,获取变量值的函数我就不贴了,太长,贴不下。
