如何在PHP Soap Class上设置成员变量?

时间:2015-10-12 08:47:44

标签: php web-services soap wsdl soap-client

我有一个类class.foo.php。它作为本地类运行正常。但是我无法在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

我可以做些什么来获得与本地类相同的结果???

以下是来自https://code.google.com/p/php-wsdl-creator/的php-wsdl-creator的Soap服务器代码

<?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
?>

1 个答案:

答案 0 :(得分:1)

经过几天的挣扎,终于找到了解决方案。我们需要调用SoapServer-&gt; setPersistence()来将soap服务器设置为持久模式。

但是php-wsdl-creator不支持此选项。所以最简单的方法是直接修改文件php-wsdl / class.phpwsdl.php。在直接调用SoapServer-&gt; handle()方法之前调用setPersistence()函数。

$this->SoapServer->setPersistence(SOAP_PERSISTENCE_SESSION);
$this->SoapServer->handle();

现在它起作用并得到了预期的结果....

Before:Original
After Set:Changed