views:

103

answers:

1

I am working on making use of a Web Services API offered by the hosts of our internal system. I am accessing it via PHP with the built-in SOAP offering.

The API session is initiated by a remote call to a function that returns some session tokens; every call to any function thereafter will return a new session token, which must accompany the next request.

I have an API Client class that is doing the bulk of the work; what I would like to do is to set something up whereby any SOAP call that is made will make sure to update the API Client class' $session variable with the new session details, and then pass the data along.

So far the only way I can think of doing this is creating a new class extending the SoapClient class, with a __call function wrapper to execute the function, update the new session token, and return the results nonetheless. I'm not sure that this will a) work b) be the best way to go about this. The wrapper class would be identical to making a SOAP call, and it would return an identical result, just it would update the session token before you get your result back.

Thanks! Hope I explained myself properly.

+2  A: 

That sounds about right, I've done something similar. You'll probably want something like:

class YourWrapperClass extends SoapClient
{
  private $session;

  private function mMakeSureSessionIsStarted()
  {
    if(!$this->session)
    {
      // code to start session here...
      $this->session = parent::startSession();
    }
  }

  public function __soapCall($method,array $arguments,array $options,$input_headers=null, array &$output_headers=null)
  {
     $this->mMakeSureSessionIsStarted();
     $arguments['session'] = $this->session;
     return parent::__soapCall($method,$arguments,$options,$input_headers,$output_headers);
  }
}

(Not 100% positive that's correct syntax, but I think it's close...)

EDIT: Revised based on your comments.

Josh
Thanks for the input!I am implementing something along these lines. Trouble that I am having is calling YourWrapperClassObj->startSession(...) goes through SoapClient::__soapCall('startSession', ...) rather than YourWrapperClass::__soapCall('startSession', ...)Not sure why this behaviour is occurring or how to take control of it.Thanks again.
Lansen Q
@Lansen: Ah, good point! I revised my answer. Don't call startSession on the object itself. Make __soapCall check to see if the session is started, and if not, start it. See my updated answer, does that make any sense?
Josh