php调用webservice的几种方法

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

2.non-WSDL模式:

2.1使用SoapParam传递参数:

  1. $ns = 'http://zhongyanfeng.com/';
  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://phpabc.cn/HelloWorld'));
  7. print_r($result2);

3.添加安全Header:

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

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: