У меня есть класс class.foo.php. Он работает нормально как локальный класс. Однако я не могу установить переменную Member под Soap Call.
Вот class.foo.php
/**
* Foo test base on PhpWsdl
*
* @service foo
*/
class Foo {
public $aMemberVar = 'Original';
/**
* setMemberVar
*
*/
function setMemberVar(){
$this->aMemberVar = 'Changed';
}
/**
* getMemberVar
*
* @return string return a String
*/
function getMemberVar(){
return $this->aMemberVar;
}
}
?>
Запуск в качестве локального класса,
<?php
require_once('class.foo.php');
$t = new Foo();
echo "<br>Before:".$t->getMemberVar();
$t->setMemberVar();
echo "<br>After Set:".$t->getMemberVar();
?>
Я получил правильный результат:
Before:Original
After Set:Changed
Однако, когда я называю это в Soap Client.
<?php
ini_set("soap.wsdl_cache_enabled", "0");
$t = new SoapClient('http://188.4.72.11/okmservices/foo.php?WSDL');
echo "<br>Before:".$t->getMemberVar();
$t->setMemberVar();
echo "<br>After Set:".$t->getMemberVar();
?>
результат непредвиден, переменная-член не изменяется:
Before:Original
After Set:Original
Что я могу сделать, чтобы получить тот же результат, что и локальный класс???
Вот база кода Soap Server на php-wsdl-creator с https://code.google.com/p/php-wsdl-creator/
<?php
require_once('class.foo.php');
// Initialize the PhpWsdl class
require_once('php-wsdl/class.phpwsdl.php');
// Disable caching for demonstration
ini_set('soap.wsdl_cache_enabled',0); // Disable caching in PHP
PhpWsdl::$CacheTime=0; // Disable caching in PhpWsdl
$soap=PhpWsdl::CreateInstance(
null, // PhpWsdl will determine a good namespace
null, // Change this to your SOAP endpoint URI (or keep it NULL and PhpWsdl will determine it)
'./cache', // Change this to a folder with write access
Array( // All files with WSDL definitions in comments
'class.foo.php'
),
null, // The name of the class that serves the webservice will be determined by PhpWsdl
null, // This demo contains all method definitions in comments
null, // This demo contains all complex types in comments
false, // Don't send WSDL right now
false); // Don't start the SOAP server right now
$soap->SoapServerOptions = Array(
'soap_version' => SOAP_1_1,
'encoding' => 'UTF-8',
'compression' => SOAP_COMPRESSION_ACCEPT);
// Run the SOAP server
if($soap->IsWsdlRequested()) // WSDL requested by the client?
$soap->Optimize=false; // Don't optimize WSDL to send it human readable to the browser
$soap->RunServer(); // Finally, run the server
?>
После нескольких дней борьбы, наконец, я нашел решение. Нам нужно вызвать SoapServer-> setPersistence(), чтобы установить режим мыльного сервера в режим сохранения.
Однако php-wsdl-creator не поддерживает эту опцию. Таким образом, самый простой способ - напрямую изменить файл php-wsdl/class.phpwsdl.php. Вызовите функцию setPersistence() перед вызовом метода SoapServer-> handle() напрямую.
$this->SoapServer->setPersistence(SOAP_PERSISTENCE_SESSION);
$this->SoapServer->handle();
Теперь он работает и получил ожидаемый результат....
Before:Original
After Set:Changed