I want to give one example for the order of commands if you want to use a class in persistence mode.
<?php
// 1. class definition or include
class UserService
{
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();
?>SoapServer::setPersistence
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
SoapServer::setPersistence
Референца за `soapserver.setpersistence.php` со подобрена типографија и навигација.
SoapServer::setPersistence
класата mysqli_driver
SoapServer::setPersistence — Поставува режим на постојаност на SoapServer
= NULL
Оваа функција овозможува менување на состојбата на постојаност на објект SoapServer помеѓу барањата. Оваа функција овозможува зачувување на податоци помеѓу барањата користејќи PHP сесии. Овој метод има ефект само на SoapServer откако ќе извезе функции користејќи SoapServer::setClass().
Забелешка:
Постојаноста на
SOAP_PERSISTENCE_SESSIONправи постојани само објекти од дадената класа, но не и статични податоци на класата. Во овој случај, користете $this->bar instead of self::$bar.
Забелешка:
SOAP_PERSISTENCE_SESSIONсеријализира податоци во објектот на класата помеѓу барањата. За правилно да се искористат ресурсите (на пр. PDO), __wakeup() and __sleep() треба да се користат магични методи.
Параметри
mode-
вистинска функција, само прототип за тоа како треба да биде функцијата.
SOAP_PERSISTENCE_*constants.SOAP_PERSISTENCE_REQUEST- Податоците на SoapServer не се постојани помеѓу барањата. Ова е default однесувањето на кој било објект SoapServer откако ќе се повика setClass.SOAP_PERSISTENCE_SESSION- Податоците на SoapServer се постојани помеѓу барањата. Ова се постигнува со серијализирање на податоците на класата SoapServer во $_SESSION['_bogus_session_name'], поради ова session_start() мора да се повика пред да се постави овој режим на постојаност.
Вратени вредности
Не се враќа вредност.
Примери
Пример #1 SoapServer::setPersistence() example
<?php
class MyFirstPersistentSoapServer {
private $resource; // (Such as PDO, mysqli, etc..)
public $myvar1;
public $myvar2;
public function __construct() {
$this->__wakeup(); // We're calling our wakeup to handle starting our resource
}
public function __wakeup() {
$this->resource = CodeToStartOurResourceUp();
}
public function __sleep() {
// We make sure to leave out $resource here, so our session data remains persistent
// Failure to do so will result in the failure during the unserialization 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->getMessage());
}
?>Белешки од корисници 6 белешки
setPersistence works 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 work at once, I started experimenting by using different settings and as stated below in the comments, "...also use SOAP_PERSISTENCE_REQUEST to save objects between requests" led me to think it was nessecary to use both modes. Well, it might for others, be but for me it turned out a day of freaking out ;) (trying all kinds of session stuff, etc etc).
Also, if persistence doesn't work, please check if session_start() is called somewhere in the script and try not to call it twice or whatsoever: it won't work...I had some issues getting session persistence (SOAP_PERSISTENCE_SESSION) to work. I finally got it working after setting session.auto_start=0, and then only calling session_start() in the script containing the SoapServer. Maybe this is obvious, but took me a bit to figure it out.
I only tried it with session.use_cookies=1, so if the settings above don't work for you, make sure cookies are enabled, though it may work without the need for cookies.Always remember to place the "setPersistence" method before the handle method, otherwise it won't work. It sounds obvious, but it's still a very common mistake, 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 page 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 gets started BEFORE the page 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.