php调用webservice的几种方法

346 篇文章 0 订阅

php调用webservice的几种方法

9151人阅读 评论(0) 收藏 举报
本文章已收录于:

1.WSDL模式:

  1. $soap = new SoapClient("http://192.168.6.69:8899/Service1.asmx?wsdl");  
  2. $result2 = $soap->HelloWorld(array(  
  3.     'myName'=>'aaa',  
  4.     'youName'=>'bbb'  
  5. ));  
  6. print_r($result2);  
$soap = new SoapClient("http://192.168.6.69:8899/Service1.asmx?wsdl");
$result2 = $soap->HelloWorld(array(
	'myName'=>'aaa',
	'youName'=>'bbb'
));
print_r($result2);


2.non-WSDL模式:

2.1使用SoapParam传递参数:

  1. $soap = new SoapClient(null,array('location'=>'http://192.168.6.72:8036/Service1.asmx','uri'=>'http://tempuri.org/'));  
  2. $result2 = $soap->__soapCall("HelloWorld",  
  3. array(new SoapParam("aaa""myName"),new SoapParam("bbb""youName")),  
  4. //array(new SoapParam("aaa", "ns1:myName"),new SoapParam("bbb", "ns1:youName")),  
  5. array('soapaction'=>'http://tempuri.org/HelloWorld'));  
  6. print_r($result2);    
$soap = new SoapClient(null,array('location'=>'http://192.168.6.72:8036/Service1.asmx','uri'=>'http://tempuri.org/'));
$result2 = $soap->__soapCall("HelloWorld",
array(new SoapParam("aaa", "myName"),new SoapParam("bbb", "youName")),
//array(new SoapParam("aaa", "ns1:myName"),new SoapParam("bbb", "ns1:youName")),
array('soapaction'=>'http://tempuri.org/HelloWorld'));
print_r($result2);	

2.2使用SoapVar传递参数

  1. $ns = 'http://tempuri.org/';  
  2. $soap = new SoapClient(null,array('location'=>'http://192.168.6.72:8036/Service1.asmx','uri'=>$ns));  
  3. $result2 = $soap->__soapCall("HelloWorld",  
  4. array(new SoapVar("AAA", XSD_STRING, null, $ns"myName"$ns),  
  5. new SoapVar("GBBB", XSD_STRING, null, $ns"youName"$ns)),  
  6. array('soapaction'=>'http://tempuri.org/HelloWorld'));  
  7. print_r($result2);  
$ns = 'http://tempuri.org/';
$soap = new SoapClient(null,array('location'=>'http://192.168.6.72:8036/Service1.asmx','uri'=>$ns));
$result2 = $soap->__soapCall("HelloWorld",
array(new SoapVar("AAA", XSD_STRING, null, $ns, "myName", $ns),
new SoapVar("GBBB", XSD_STRING, null, $ns, "youName", $ns)),
array('soapaction'=>'http://tempuri.org/HelloWorld'));
print_r($result2);


3.添加安全Header

  1. $soap = new SoapClient(null,array('location'=>'http://192.168.6.47/onvif/device_service','uri'=>'http://www.onvif.org/ver10/device/wsdl/'));  
  2. //ws  
  3. $ns_wsse = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";//WS-Security namespace  
  4. $ns_wsu = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";//WS-Security namespace  
  5.   
  6. $userT = new SoapVar('admin', XSD_STRING, NULL, $ns_wsse, NULL, $ns_wsse);  
  7. $passwT = new SoapVar('NnYZe7oD81Kd8QRS4tUMze/2CUs=', XSD_STRING, NULL, $ns_wsse, NULL, $ns_wsse);  
  8. $createdT = new SoapVar(time(), XSD_DATETIME, NULL, $ns_wsu, NULL, $ns_wsu);  
  9. class UsernameT1 {  
  10. private $Username;   
  11. //Name must be  identical to corresponding XML tag in SOAP header  
  12. private $Password;   
  13. // Name must be  identical to corresponding XML tag in SOAP header   
  14. private $Created;  
  15.   function __construct($username$password$created) {  
  16.          $this->Username=$username;  
  17.          $this->Password=$password;  
  18.          $this->Created=$created;  
  19.     }  
  20. }  
  21. $tmp = new UsernameT1($userT$passwT$createdT);  
  22. $uuT = new SoapVar($tmp, SOAP_ENC_OBJECT, NULL,   
  23. $ns_wsse'UsernameToken'$ns_wsse);  
  24.   
  25. class UserNameT2 {  
  26. private $UsernameToken;    
  27. //Name must be  identical to corresponding XML tag in SOAP header  
  28. function __construct ($innerVal){  
  29.     $this->UsernameToken = $innerVal;  
  30. }  
  31. }  
  32. $tmp = new UsernameT2($uuT);  
  33. $userToken = new SoapVar($tmp, SOAP_ENC_OBJECT, NULL, $ns_wsse'UsernameToken'$ns_wsse);  
  34.   
  35. $secHeaderValue=new SoapVar($userToken, SOAP_ENC_OBJECT, NULL,   
  36.                                         $ns_wsse'Security'$ns_wsse);  
  37. $secHeader = new SoapHeader($ns_wsse'Security'$secHeaderValue);  
  38. $result2 = $soap->__soapCall("GetDeviceInformation",array(),null,$secHeader);  
  39. echo $result2;  
$soap = new SoapClient(null,array('location'=>'http://192.168.6.47/onvif/device_service','uri'=>'http://www.onvif.org/ver10/device/wsdl/'));
//ws
$ns_wsse = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";//WS-Security namespace
$ns_wsu = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd";//WS-Security namespace

$userT = new SoapVar('admin', XSD_STRING, NULL, $ns_wsse, NULL, $ns_wsse);
$passwT = new SoapVar('NnYZe7oD81Kd8QRS4tUMze/2CUs=', XSD_STRING, NULL, $ns_wsse, NULL, $ns_wsse);
$createdT = new SoapVar(time(), XSD_DATETIME, NULL, $ns_wsu, NULL, $ns_wsu);
class UsernameT1 {
private $Username; 
//Name must be  identical to corresponding XML tag in SOAP header
private $Password; 
// Name must be  identical to corresponding XML tag in SOAP header 
private $Created;
  function __construct($username, $password, $created) {
	     $this->Username=$username;
	     $this->Password=$password;
	     $this->Created=$created;
    }
}
$tmp = new UsernameT1($userT, $passwT, $createdT);
$uuT = new SoapVar($tmp, SOAP_ENC_OBJECT, NULL, 
$ns_wsse, 'UsernameToken', $ns_wsse);

class UserNameT2 {
private $UsernameToken;  
//Name must be  identical to corresponding XML tag in SOAP header
function __construct ($innerVal){
	$this->UsernameToken = $innerVal;
}
}
$tmp = new UsernameT2($uuT);
$userToken = new SoapVar($tmp, SOAP_ENC_OBJECT, NULL, $ns_wsse, 'UsernameToken', $ns_wsse);

$secHeaderValue=new SoapVar($userToken, SOAP_ENC_OBJECT, NULL, 
                                        $ns_wsse, 'Security', $ns_wsse);
$secHeader = new SoapHeader($ns_wsse, 'Security', $secHeaderValue);
$result2 = $soap->__soapCall("GetDeviceInformation",array(),null,$secHeader);
echo $result2;




1
0


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值