Why the PHP SOAP session is not persistent?
I defined a SOAP Server in the following way.
ini_set('soap.wsdl_cache_enabled', 0);
ini_set('session.auto_start', 0);
global $config_dir;
session_start();
$server = new SoapServer("xxxx.wsdl");
$server->setClass('import');
$server->setPersistence(SOAP_PERSISTENCE_SESSION);
$server->handle();
When I connect it with a SoapClient and calling the public functions, the session seems not persistent. I set a private variable on a first call but thi开发者_开发知识库s variable looses its value on the second call....
$client = new SoapClient('xxxx.wsdl', array('trace' => 1, 'exceptions' => 1));
//The First call. I set the fromdb private variable to "nostran"
$client->setFromdb("nostran");
//The second call. Here I need the fromdb but it has not any value....
$b = $client->setIrodak_id("1234");
Any thoughts? Thank you!
_bogus_session_name
seems to be the key in the session array that soap extension uses (see the source).
The usual advice on objects stored in the session apply: make sure the class definition is available before calling session_start or that you have an autoload handler set before calling handle
.
The SOAP library in PHP does not keep cookies between requests per default. You need to manually fetch them with getLastResponseHeaders() and set them with __setCookie().
First you can try to always set the SoapHeader like this:
$header = new SoapHeader('{NAMESPACE__YOU_CAN_FIND_IT_IN_YOUR_WSDL__TARGETNAMESPACE}', 'SessionPersistenceHeader');
$client = new SoapClient('xxxx.wsdl', array('trace' => 1, 'exceptions' => 1));
$client->__setSoapHeaders($header);
The SoapHeader second parameter is chosen freely and the third parameter is optional and could be empty to solve this problem. Just add an empty header.
...and second you shouldn't make a session_start()
before initializing your Server.
...and third try to not set your php.ini-settings on runtime but in your php.ini itself.
Hint: When you use Zend/Soap/Client.php from Zend Framework 2 the SoapHeader will be always set. That solves it for me.
TESTs: The best way to test your SoapServer on SOAP_PERSISTENCE_SESSIONs
is to may a class Variable, a setter and a getter for it.
Here the example-class:
<?php
class soaptest {
/**
* var
*
* @var int
* @access private
*/
private $_var = 0;
/**
* setVar
*
* @param int $value
* @access public
* @return void
*/
public function setVar($value) {
$this->_var = $value;
}
/**
* getVar
*
* @access public
* @return int
*/
public function getVar() {
return $this->_var;
}
}
?>
and than in your TestClient:
<?php
// [...]
$value = 123;
$client->setVar($value);
$result = $client->getVar();
if ($value == $result) {
echo 'SoapAPI is session persistent.';
} else {
echo 'SoapAPI is NOT session persistent.';
}
?>
精彩评论