当前位置:   article > 正文

php调用webservice的几种方法_php调webserveγ

php调webserveγ

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);  
  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);


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);    
  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);

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);  
  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);


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;  
  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. $userT = new SoapVar('admin', XSD_STRING, NULL, $ns_wsse, NULL, $ns_wsse);
  6. $passwT = new SoapVar('NnYZe7oD81Kd8QRS4tUMze/2CUs=', XSD_STRING, NULL, $ns_wsse, NULL, $ns_wsse);
  7. $createdT = new SoapVar(time(), XSD_DATETIME, NULL, $ns_wsu, NULL, $ns_wsu);
  8. class UsernameT1 {
  9. private $Username;
  10. //Name must be identical to corresponding XML tag in SOAP header
  11. private $Password;
  12. // Name must be identical to corresponding XML tag in SOAP header
  13. private $Created;
  14. function __construct($username, $password, $created) {
  15. $this->Username=$username;
  16. $this->Password=$password;
  17. $this->Created=$created;
  18. }
  19. }
  20. $tmp = new UsernameT1($userT, $passwT, $createdT);
  21. $uuT = new SoapVar($tmp, SOAP_ENC_OBJECT, NULL,
  22. $ns_wsse, 'UsernameToken', $ns_wsse);
  23. class UserNameT2 {
  24. private $UsernameToken;
  25. //Name must be identical to corresponding XML tag in SOAP header
  26. function __construct ($innerVal){
  27. $this->UsernameToken = $innerVal;
  28. }
  29. }
  30. $tmp = new UsernameT2($uuT);
  31. $userToken = new SoapVar($tmp, SOAP_ENC_OBJECT, NULL, $ns_wsse, 'UsernameToken', $ns_wsse);
  32. $secHeaderValue=new SoapVar($userToken, SOAP_ENC_OBJECT, NULL,
  33. $ns_wsse, 'Security', $ns_wsse);
  34. $secHeader = new SoapHeader($ns_wsse, 'Security', $secHeaderValue);
  35. $result2 = $soap->__soapCall("GetDeviceInformation",array(),null,$secHeader);
  36. echo $result2;




1
0


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/65957
推荐阅读
  

闽ICP备14008679号