PHP5: how do I reuse SoapClient (session persistence)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pctwo
Forum Newbie
Posts: 2
Joined: Mon Mar 26, 2007 4:29 pm

PHP5: how do I reuse SoapClient (session persistence)

Post by pctwo »

Hi

I'm writing a php5 app that uses the buil-in SoapClient class to consume a .NET soap service. The soap service has session support. Here's a sample response

Code: Select all

POST /xxxxxxx/xxxxxx.asmx HTTP/1.1
Host: xxx.xxx.xxx.xxx:xxxx
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.1.6
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://......"
Content-Length: 238
Cookie: ASP.NET_SessionId=4l45ah4xxxxxxxxxxxxxxxx;
My app works fine except that I have to create a new SoapClient every time. The user requests something from my server, I create a soapclient, connect to the soap service, authenticate, make request, send reply back to user. User requests something else from my server, I create a soapclient, connect, etc ... and do the whole thing again.

I know how to use php's session handling to track my user. And the soap service is tracking ME with ITS session. What I need is to be able to reuse the soapclient or at least be able to tell the new soapclient to reuse the same session. I thought of saving the client in $_SESSION, but of course that doesn't work because there are two resource variables (sdl and httpurl). I can save soapclient['_cookies'] but I don't know how to use it and whether that'll actually work.

help? thanks in advance.
pctwo
Forum Newbie
Posts: 2
Joined: Mon Mar 26, 2007 4:29 pm

Post by pctwo »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I figured this out.  After the initial connection, I need to save the soapclient's _cookies in my session.  The soapclient's structure looks like this

Code: Select all

SoapClient Object
(
    [trace] => 1
    [_soap_version] => 1
    [sdl] => Resource id #3
    [_cookies] => Array
        (
            [ASP.NET_SessionId] => Array
                (
                    [0] => b12bts550fe5nrxhk45
                )
        )
}
Only _cookies needs to be saved. Then on subsequent connections, create the soapclient then use the __setCookie method to set the cookie saved in the session. Code looks roughly like this

Code: Select all

function getClient() {	
		$client = new SoapClient('http://host/service.wsdl');
                                // is soapcookies already set? (must be already logged in)
		if (isset($_SESSION['soapcookies']) {
                                                // just set the cookies
			foreach ($_SESSION['soapcookies'] as $cookiename=>$value)
				$client->__setCookie($cookiename,$value[0]);
		} else {
                                                dologin($client);  // do whatever needed to initiate the connection.
                                                // save the _cookies
                                                $_SESSION['soapcookies'] = $client->_cookies;
                                }
		return $client;
	}
Obviously, you'd want to add some error checking, but that's the gist of it.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply