[英]How to set member variables on PHP Soap Class?
我有一个类class.foo.php。 它作为本地类正常运行。 但是,我不能在Soap Call下设置Member Variable。
这是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
我该怎么做才能获得与本地课程相同的结果???
这是基于https://code.google.com/p/php-wsdl-creator/的 php-wsdl-creator的Soap Server代码。
<?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。 在直接调用SoapServer-> handle()方法之前,请先调用setPersistence()函数。
$this->SoapServer->setPersistence(SOAP_PERSISTENCE_SESSION);
$this->SoapServer->handle();
现在它可以正常工作并获得预期的结果。
Before:Original
After Set:Changed
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.