网站查了一些SoapClient资料。
DEMO如下:一.客户端soapClient.php:
<?php
try {
//non-wsdl方式调用WebService
$soap = new SoapClient(null, array(
'location'=> "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:
<?php
//non-wsdl方式提供WebService(指定相应的uri)
$server = new SoapServer(null,array("uri"=>"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
DEMO如下:一.客户端soapClient.php:
<?php
try {
//non-wsdl方式调用WebService
$soap = new SoapClient(null, array(
'location'=> "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:
<?php
//non-wsdl方式提供WebService(指定相应的uri)
$server = new SoapServer(null,array("uri"=>"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
$soap = new SoapClient("http://www.XXX.com/ipcam/soapservice/math.wsdl");
$result1 = $soap->addition ( 200, 160 );
echo $result1;
第三方不提供WSDL。所以用SoapClient能用吗?
除了PHP的SoapClient方法通信外是不是可以直接用HTTP请求来完成通信?
注:第三方提供了SOAP消息的结构。第三方反馈的信息也是SOAP信息结构。HTTP请求时加上SOAP的消息结构
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"><soap:Header>
...
...
</soap:Header><soap:Body>
...
...
<soap:Fault>
...
...
</soap:Fault>
</soap:Body></soap:Envelope>
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("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
soap.Append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:ns1=\"http://www.liveipc.com/UserService/\">\n");
soap.Append("<SOAP-ENV:Body>");
soap.Append("<ns1:UserOperation>");
soap.Append("<WSRequest>");
soap.Append("<Version>" + Version + "</Version>");
soap.Append("<CharSet>GBK</CharSet>");
soap.Append("<TransactionName>UserRegistration</TransactionName>");
soap.Append("<Random>" + strRandom + "</Random>");
soap.Append("<Name>" + DeviceName + "</Name>");
soap.Append("<DevAttr>" + DeviceAttr + "</DevAttr>");
soap.Append("<SoapConfigData></SoapConfigData>");
soap.Append("<OEMID>" + OEMID + "</OEMID>");
soap.Append("<UserName>" + UserName + "</UserName>");
soap.Append("<Password>" + Password + "</Password>");
soap.Append("<ConfirmPassword>" + ConfirmPassword + "</ConfirmPassword>");
soap.Append("<EMailBox>" + EMailBox + "</EMailBox>");
soap.Append("</WSRequest>");
soap.Append("</ns1:UserOperation>");
soap.Append("</SOAP-ENV:Body>");
soap.Append("</SOAP-ENV:Envelope>"); 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;
}
还有一些变量的定义,获取变量值的函数我就不贴了,太长,贴不下。