关于SoapClient问题

原创
2016-06-23 14:13:40 1369浏览

网站查了一些SoapClient资料。
DEMO如下:

一.客户端soapClient.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:
//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


回复讨论(解决方案)

需要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;    }

还有一些变量的定义,获取变量值的函数我就不贴了,太长,贴不下。

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。