views:

368

answers:

1

I have a generally straight forward web service that I've written (converting code to ZF from a Java implementation of the same service and trying to maintain the same wsdl structure as much as possible). The service loads a PHP class, rather than individual functions. The PHP class contains three different functions within it.

Everything seems to be working just fine, except that I can't seem to figure out how to specify that a given function parameter should be passed as a SOAP header. I've not seen any mention of SOAP headers in the Server context, only how to pass header parameters with a client to a server.

In addition to the standard parameters for the function that would be sent in the SOAP body and detailed in the docblock, I would like to specify two parameters (a username and password) that would be sent in a SOAP header.

I have to assume this is possible, but haven't been able to find anything online, nor have I had any responses to a similar post on Zend's forum. Is there something that can be added in the docblock area to specify a parameter as a header (maybe in a similar fashion to using WebParam?)? Any suggestions/examples on how to get this accomplished would be greatly appreciated!

+1  A: 

I just ran into this problem myself. My SOAP request is structured like so:

<SOAP-ENV:Envelope>
  <SOAP-ENV:Header>
    <Header>
      <APIKey>$key</APIKey>
      <SiteID>$id</SiteID>
    </Header>
  </SOAP-ENV:HEADER>
  (body)
</SOAP-ENV:Envelope>

Because the contents of my <SOAP-ENV:Header> tag are in the <Header> enclosure, I created a public method in the class my SoapServer instance loads called Header that then sets a private class variable to true if the API key and Site ID are valid. The other methods in my class that process the body of the request then check to see if that variable is true before proceeding. Ugly, I know, but as you mention, there's no documentation, and this seems to be the easiest way. It looks like this:

class MySoapRequestHandler
{
    private $authenticated;

    public function Header($data)
    {
      //your logic here
      if($request_is_valid)
      {
        $this->authenticated = true;
      }
      else
      {
        $this->authenticated = false;
      }
    }

    public function ProcessBody($data) //of course named whatever your body element is named
    {
      if($this->authenticated === true)
      {
        //process the request
      }
      else
      {
        //throw a soap fault?
      }
    }
  }

Let me know if you have more questions; happy to help as much as I can.

benjy