关于SoapClient问题
程序员文章站
2022-05-23 15:19:17
...
网站查了一些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
...
...
...
...
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的消息结构
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(" "); byte[] paramBytes = Encoding.UTF8.GetBytes(soap.ToString()); for (int j = 0; j"); soap.Append(" "); 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("
还有一些变量的定义,获取变量值的函数我就不贴了,太长,贴不下。