找了一整天,原来服务端没什么特别要做的,就是像普通的webservice一样提供方法就可以。下面是客户端的实现:<?php class authentication_header { private $username; private $password; public function __construct($username, $password) { $this->username = $username; $this->password = $password; } } // generate new object $auth = new authentication_header('123', '123'); // create authentication header values $authvalues = new SoapVar($auth, SOAP_ENC_OBJECT, 'authenticate'); // generate header $header = new SoapHeader('http://78diy.org/', 'authenticate', $authvalues); $client = new SoapClient('http://127.0.0.1:8000/webservice/test/serve.php?wsdl'); $client->__setSoapHeaders(array($header)); echo $client->__soapCall('felineResponse', array()); ?> 如下是服务器端的实现:<?php class mysoapclass { /* * Authentication function * This is called by the Soap header of the same name. The function name is a Ws-security standard auth tag * and corresponds to the header tag. * * @param string username * @param string password */ public function authenticate($username, $password){ // Store username for logging $this->Username = $username; if($username == '123') { $this->Authenticated = true; } else { $this->Authenticated = false; } } /* * Test method */ public function felineResponse(){ // Place this at the start of every exposed method // This is because the SoapServer will attempt to call // if no authentication headers are passed. if($this->Authenticated){ return 'zheng que'; } else { return 'cuo wu'; } } } // Instantiate server with relevant wsdl & class. $server = new SoapServer( 'mysoapwsdl.wsdl' ); $server->setClass('mysoapclass'); $server->handle(); ?>