简体   繁体   English

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

[英]How to set member variables on PHP Soap Class?

I have a class class.foo.php. 我有一个类class.foo.php。 It runs normal as local class. 它作为本地类正常运行。 However I cannot set the Member Variable under Soap Call. 但是,我不能在Soap Call下设置Member Variable。

Here is class.foo.php 这是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;
    }


} 
?>

Running as local class, 作为本地课程,

<?php
require_once('class.foo.php');
$t = new Foo();
echo "<br>Before:".$t->getMemberVar();
$t->setMemberVar();
echo "<br>After Set:".$t->getMemberVar();
?>

I got correct result: 我得到正确的结果:

Before:Original
After Set:Changed

However when I call it in Soap Client. 但是,当我在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();
?>

the result is unexpected, the member variable does not change: 结果是意外的,成员变量不会更改:

Before:Original
After Set:Original

What can I do to get same result as local class??? 我该怎么做才能获得与本地课程相同的结果???

Here is the Soap Server Code base on php-wsdl-creator from https://code.google.com/p/php-wsdl-creator/ 这是基于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
?>

After few days struggles, finally i found the solution. 经过几天的努力,终于找到了解决方案。 We need to call the SoapServer->setPersistence() to set the soap server to persistence mode. 我们需要调用SoapServer-> setPersistence()来将肥皂服务器设置为持久性模式。

However php-wsdl-creator does not support this option. 但是php-wsdl-creator不支持此选项。 So the easiest way is to modify the file php-wsdl/class.phpwsdl.php directly. 因此,最简单的方法是直接修改文件php-wsdl / class.phpwsdl.php。 Call the setPersistence() function before calling the SoapServer->handle() method directly. 在直接调用SoapServer-> handle()方法之前,请先调用setPersistence()函数。

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

Now it works and got the expected result.... 现在它可以正常工作并获得预期的结果。

Before:Original
After Set:Changed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM