Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 20 ■ ADVANCED WEB SERVICES^307

//Get the session id from the client cookies
$session_id = $client->_cookies['PHPSESSID'][0];

/*
Sometime later, like in another request,
you might create a new SoapClient.
*/
$client2 = new SoapClient('demo.wsdl');

/*
To resume the session, set the session
id for client2 manually using __setCookie
*/
$client2->__setCookie('PHPSESSID', $session_id);

echo $client2->demo('a'); //3
echo $client2->demo('a'); //4

/*
A new client without setting the session
cookie will start a new session
*/
$client3 = new SoapClient('demo.wsdl');
echo $client3->demo('a'); //1

12341

Listing 20-7. A Session-Based SOAP Server (service.php)

<?php

function Demo($param1) {
session_start();
if(!array_key_exists('counter', $_SESSION)) {
$_SESSION['counter'] = 1;
}
return $_SESSION['counter']++;
}

$server = new SoapServer('demo.wsdl');
$server->addFunction('Demo');
$server->handle();

This ability to get and set the session ID manually is very important. It is most commonly
used when you are creating some sort of multipage process, like an order or sign-up procedure.
In these cases, you may start a web service session on the first page and call methods in that
same session with each additional post from your browser.

McArthur_819-9.book Page 307 Friday, February 29, 2008 8:03 AM

Free download pdf