昨天還在抱怨採用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認證 請看本人另一篇文章