note 78872 added to function.soap-soapserver-construct

From: Date: Wed, 31 Oct 2007 11:42:35 +0000
Subject: note 78872 added to function.soap-soapserver-construct
Groups: php.notes 
Request: Send a blank email to php-notes+get-131859@lists.php.net to get a copy of this message
In response to Timo, it is possible to access Soap Headers from the SoapServer class and call methods to handle them. Rather than treat them seperately, they are treated as part of the Soap request. To pass approximately wss standard headers (note that I couldn't get attributes from the header tags without text preprocessing, so standards compliance failed me), first create the following class on the client: <?php /* * Login credentials to be supplied as a SOAP header * Class used to ensure Soap Head tags in correct format. */ class SoapHeaderUsernameToken { /** @var int Password */ public $Password; /** @var int Username */ public $Username; public function __construct($l, $p) { $this->Password = $p; $this->Username = $l; } } ?> Now, set the Soap Headers: <?php // Set the login headers $wsu = 'http://schemas.xmlsoap.org/ws/2002/07/utility'; $usernameToken = new SoapHeaderUsernameToken($this->username, $this->password); $soapHeaders[] = new SoapHeader($wsu, 'UsernameToken', $usernameToken); ?> Next instantiate the client object, add the headers and make the call: <?php $client = new SoapClient( $wsdl); $client->__setSoapHeaders( $soapHeaders ); $client->__soapCall( $method, $params ); ?> Moving to the Server, you'll handle the Soap call somewhat like: <?php // Instantiate server with relevant wsdl & class. $server = new SoapServer( 'mysoapwsdl.wsdl' ); $server->setClass( 'mysoapclass' ); $server->handle(); ?> The key here is that the headers will be handled first by the server which will call the method mysoapclass::UsernameToken. The way we work this is the UsernameToken method authenticates the username / password combo and sets a protected class var. Then when the Soap Body is handled and the appropriate class method called, a check is made at the start of callable method to ensure authentication has been passed. If the Soap auth header is missing, the soap body method will be called anyway so the check is essential in case a malevolent client deliberately leaves the headers off. An example server class is below (missing various methods which I leave as an exercise to the reader to provide): <?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 UsernameToken( $username, $password ){ // Store username for logging $this->Username = $username; $auth = new $this->AuthClass( $username, $password, get_class($this) ); if( $auth->IsValid() ){ $this->Authenticated = true; } else { $this->ThrowSoapFault( 'auth' ); } } /* * Test method */ public function felineResponse( $action ){ // 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){ $this->ThrowSoapFault( 'auth' ); } if( $action == 'stroke' ){ return 'purr'; } elseif( $action == 'tease' ){ return 'hiss'; } } } ?> This is a adaptation of stuff I found online somewhere... ---- Server IP: 194.145.210.4 Probable Submitter: 213.249.149.102 ---- Manual Page -- http://www.php.net/manual/en/function.soap-soapserver-construct.php Edit -- https://master.php.net/note/edit/78872 Del: integrated -- https://master.php.net/note/delete/78872/integrated Del: useless -- https://master.php.net/note/delete/78872/useless Del: bad code -- https://master.php.net/note/delete/78872/bad+code Del: spam -- https://master.php.net/note/delete/78872/spam Del: non-english -- https://master.php.net/note/delete/78872/non-english Del: in docs -- https://master.php.net/note/delete/78872/in+docs Del: other reasons-- https://master.php.net/note/delete/78872 Reject -- https://master.php.net/note/reject/78872 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#131859) next »