[英]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.