简体   繁体   中英

How to set member variables on PHP Soap Class?

I have a class class.foo.php. It runs normal as local class. However I cannot set the Member Variable under Soap Call.

Here is 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.

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

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

However php-wsdl-creator does not support this option. So the easiest way is to modify the file php-wsdl/class.phpwsdl.php directly. Call the setPersistence() function before calling the SoapServer->handle() method directly.

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

Now it works and got the expected result....

Before:Original
After Set:Changed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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