webservice soapheader 身份認證 wsdl方式

來源:互聯網
上載者:User

昨天還在抱怨採用laruence大哥soapheader 身份認證沒有成功。今天終於成功了

client.php

<?php<br />class SoapUserInfo {<br /> /**<br /> * @var char $name<br /> */<br /> public $name;<br /> /**<br /> * @var char $password<br /> */<br /> public $password;<br /> public function __construct($l, $p) {<br /> $this->password = $p;<br /> $this->username = $l;<br /> }<br />}<br />ini_set("soap.wsdl_cache_enabled", "0");<br />try {<br />$auth =new SoapUserInfo('laruence', 'password');<br />$authvalues = new SoapVar($auth, SOAP_ENC_OBJECT, 'Authentication','http://test.cheshi.com');<br />$soap_header = new SoapHeader("http://test.cheshi.com", 'Authentication', $authvalues, true, SOAP_ACTOR_NEXT);</p><p>$client = new SoapClient('http://test.cheshi.com/webservice/laruence.wsdl');<br />$re =$client->__setSoapHeaders(array($soap_header));</p><p>echo $result =$client->__soapCall('request', array());</p><p>}catch (SoapFault $e){<br />echo $e->getMessage();<br />}<br /> ?><br />

server.php

 <?php<br />ini_set("soap.wsdl_cache_enabled", "0");<br />class InterfaceClass {<br /> /**<br /> * @var bool $authorized<br /> */<br /> private $authorized = FALSE;<br /> /*<br /> * Authentication function<br /> *<br /> * @param string username<br /> * @param string password<br /> */<br /> public function Authentication($username,$password) {</p><p> if($username =='laruence'&&$password=='password'){<br /> $this->authorized = true;</p><p> }<br /> }<br /> /*<br /> * Test method<br /> */<br /> public function request(){</p><p> if ($this->authorized) {<br /> return '成功'; //驗證成功, 繼續處理.<br /> } else {<br /> return '下次成功'; //驗證失敗, 拒絕請求.<br /> }<br /> }<br />}</p><p>$server = new SoapServer('laruence.wsdl');<br />$server->setClass('InterfaceClass');<br />$server->handle();<br />?>

laruence.wsdl

<?xml version="1.0" encoding="UTF-8"?><br /><wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test.cheshi.om/laruence/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="laruence" targetNamespace="http://test.cheshi.om/laruence/"><br /> <wsdl:types><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://test.cheshi.om/laruence/"><br /><xsd:element name="Authentication"><br /><xsd:complexType><br /><xsd:sequence></p><p><xsd:element name="in" type="xsd:string"></xsd:element><br /></xsd:sequence><br /></xsd:complexType><br /></xsd:element></xsd:schema></wsdl:types><br /> <wsdl:message name="requestRequest"></p><p> </wsdl:message><br /> <wsdl:message name="requestResponse"><br /> <wsdl:part name="requestResponse" type="xsd:string"/><br /> </wsdl:message><br /> <wsdl:message name="AuthenticationRequest"><br /> <wsdl:part name="username" type="xsd:string"></wsdl:part><br /> <wsdl:part name="password" type="xsd:string"></wsdl:part><br /> </wsdl:message><br /> <wsdl:message name="AuthenticationResponse"></p><p> </wsdl:message><br /> <wsdl:portType name="laruence"><br /> <wsdl:operation name="request"><br /> <wsdl:input message="tns:requestRequest"/><br /> <wsdl:output message="tns:requestResponse"/><br /> </wsdl:operation><br /> <wsdl:operation name="Authentication"><br /> <wsdl:input message="tns:AuthenticationRequest"></wsdl:input><br /> <wsdl:output message="tns:AuthenticationResponse"></wsdl:output><br /> </wsdl:operation><br /> </wsdl:portType><br /> <wsdl:binding name="laruenceSOAP" type="tns:laruence"><br /> <soap:binding style="rpc" mce_style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/><br /> <wsdl:operation name="request"><br /> <soap:operation soapAction="http://test.cheshi.om/laruence/request"/><br /> <wsdl:input><br /> <soap:body namespace="http://test.cheshi.om/laruence/" use="literal"/><br /> </wsdl:input><br /> <wsdl:output><br /> <soap:body namespace="http://test.cheshi.om/laruence/" use="literal"/><br /> </wsdl:output><br /> </wsdl:operation><br /><wsdl:operation name="Authentication"><br /> <soap:operation soapAction="http://test.cheshi.om/laruence/request"/><br /> <wsdl:input><br /> <soap:body namespace="http://test.cheshi.om/laruence/" use="literal"/><br /> </wsdl:input><br /> <wsdl:output><br /> <soap:body namespace="http://test.cheshi.om/laruence/" use="literal"/><br /> </wsdl:output><br /> </wsdl:operation><br /> </wsdl:binding><br /> <wsdl:service name="laruence"><br /> <wsdl:port binding="tns:laruenceSOAP" name="laruenceSOAP"><br /> <soap:address location="http://test.cheshi.com/webservice/server.php"/><br /> </wsdl:port><br /> </wsdl:service><br /></wsdl:definitions>

 原來不成功是因為 本人在使用zend studio 產生wsdl時

缺少一部分代碼,只綁定了 一個方法 ,如下部分一般只能產生一種方法的綁定

其他要自己手動來綁。如果有人知道為什麼,希望告知本人。謝謝

<wsdl:binding name="laruenceSOAP" type="tns:laruence"><br /> <soap:binding style="rpc" mce_style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/><br /> <wsdl:operation name="request"><br /> <soap:operation soapAction="http://test.cheshi.om/laruence/request"/><br /> <wsdl:input><br /> <soap:body namespace="http://test.cheshi.om/laruence/" use="literal"/><br /> </wsdl:input><br /> <wsdl:output><br /> <soap:body namespace="http://test.cheshi.om/laruence/" use="literal"/><br /> </wsdl:output><br /> </wsdl:operation><br /><wsdl:operation name="Authentication"><br /> <soap:operation soapAction="http://test.cheshi.om/laruence/request"/><br /> <wsdl:input><br /> <soap:body namespace="http://test.cheshi.om/laruence/" use="literal"/><br /> </wsdl:input><br /> <wsdl:output><br /> <soap:body namespace="http://test.cheshi.om/laruence/" use="literal"/><br /> </wsdl:output><br /> </wsdl:operation><br /> </wsdl:binding>

 

nonwdsl方式的soapheader認證 請看本人另一篇文章

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.