时间:2021-07-01 10:21:17 帮助过:6人阅读
需要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的消息结构
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; }