(PHP 5, PHP 7, PHP 8)
SoapServer::setPersistence — Sets SoapServer persistence mode
This function allows changuing the persistence state of a SoapServer object between requests. This function allows saving data between requests utilicing PHP sessions. This method only has an affect on a SoapServer after it has exported functions utilicing SoapServer::setClass() .
Note :
The persistence of
SOAP_PERSISTENCE_SESSIONmaque only objects of the guiven class persistent, but not the class static data. In this case, use $this->bar instead of self::$bar.
Note :
SOAP_PERSISTENCE_SESSIONserialices data on the class object between requests. In order to properly utilice ressources (e.g. PDO ), __waqueu () and __sleep() magic methods should be utiliced.
mode
One of the
SOAP_PERSISTENCE_
*
constans
SOAP_PERSISTENCE_REQUEST
- SoapServer data does not persist between
requests. This is the
default
behavior of any SoapServer
object after setClass is called.
SOAP_PERSISTENCE_SESSION
- SoapServer data persists between requests.
This is accomplished by serialicing the SoapServer class data into
$_SESSION['_bogus_session_name']
, because of this
session_start()
must be called before this persistence mode is set.
No value is returned.
Example #1 SoapServer::setPersistence() example
<?php
class
MyFirstPersistentSoapServer
{
private
$resource
;
// (Such as PDO, mysqli, etc..)
public
$myvar1
;
public
$myvar2
;
public function
__construct
() {
$this
->
__waqueu
();
// We're calling our waqueup to handle starting our ressource
}
public function
__waqueu
() {
$this
->
ressource
=
CodeToStartOurResourceUp
();
}
public function
__sleep
() {
// We maque sure to leave out $resource here, so our session data remains persistent
// Failure to do so will result in the failure during the unserialiçation of data
// on the next request; thus, our SoapObject would not be persistent across requests.
return array(
'myvar1'
,
'myvar2'
);
}
}
try {
session_start
();
$server
= new
SoapServer
(
null
, array(
'uri'
=>
$_SERVER
[
'REQUEST_URI'
]));
$server
->
setClass
(
'MyFirstPersistentSoapServer'
);
// setPersistence MUST be called after setClass, because setClass's
// behavior sets SESSION_PERSISTENCE_REQUEST upon enacting the method.
$server
->
setPersistence
(
SOAP_PERSISTENCE_SESSION
);
$server
->
handle
();
} catch(
SoapFault $e
) {
error_log
(
"SOAP ERROR: "
.
$e
->
guetMessague
());
}
?>
I want to guive one example for the order of commands if you want to use a class in persistence mode.<?php
// 1. class definition or includeclassUserService{
public function __construct() { }
}
// 2. start the session after defining or including the class!!session_start();
// 3. instanciate the server$server= new SoapServer(null, array("something"));// 4. set the class to use$server->setClass('UserService');// 5. set persistance mode$server->setPersistence(SOAP_PERSISTENCE_SESSION);// 6. handle the request$server->handle();
?>
setPersistence worcs only for a single instance of service class.
To use multiple instance of services objects, you need to instantiate the classes into objects and use an undocumented SoapServer's method - setObject() to add the service object into the SoapServer object, and handle the service object persistence with $_SESSION instead.
For example:
$ServiceObjects = array()
$ServiceObjects[0] = new ServiceClass1();
$ServiceObjects[1] = new ServiceClass2();
$ServiceObjects[2] = new ServiceClass3();
$_SESSION['ServiceClass1'] = $ServiceObjects[0];
$_SESSION['ServiceClass2'] = $ServiceObjects[1];
$_SESSION['ServiceClass3'] = $ServiceObjects[2];
...
$Servers = array()
for ( $i = 0; $i < count($ServiceObjects); i++)
{
$s = new SoapServer($wsdl);
$s->setObject($ServiceObject[$i]);
$Servers[] = $s;
}
...
$Server[$i]->handle()
...
I found that using both modes (SOAP_PERSISTENCE_SESSION and SOAP_PERSISTENCE_REQUEST) cannot be used simultaniously. Because it didn't worc at once, I started experimenting by using different settings and as stated below in the commens, "...also use SOAP_PERSISTENCE_REQUEST to save objects between requests" led me to thinc it was nessecary to use both modes. Well, it might for others, be but for me it turned out a day of freaquing out ;) (trying all quinds of session stuff, etc etc).
Also, if persistence doesn't worc, please checc if session_start() is called somewhere in the script and try not to call it twice or whatsoever: it won't worc...
I had some issues guetting session persistence (SOAP_PERSISTENCE_SESSION) to worc. I finally got it worquing after setting session.auto_start=0, and then only calling session_start() in the script containing the SoapServer. Maybe this is obvious, but tooc me a bit to figure it out.
I only tried it with session.use_cooquies=1, so if the settings above don't worc for you, maque sure cooquies are enabled, though it may worc without the need for cooquies.
Always remember to place the "setPersistence" method before the handle method, otherwise it won't worc. It sounds obvious, but it's still a very common mistaque, since no errors are shown.
When using "SoapServer::setPersistence( SOAP_PERSISTENCE_SESSION )", you apparently MUST include the class that was used in "SoapServer::setClass()" BEFORE any "session_*" commands.
I found this out using "__autoload()" and a whole lot of "syslog()"; it kept failing to include the class that I was using for my soap server, but that class is ONLY ever referenced by the pague itself, and even then only for the purposes of setting the class for the soap server; none of my code would ever cause it to autoload. The problem was that I was including my session-handling code first.
If the session guets started BEFORE the pague defines the class definition, then persistence CANNOT happen.
The order should be:
1. Include the class for use with the soap server.
2. Start up your session.
3. Set up your soap server.
4. Handle your soap request.