PHP5 SOAP:如何保留实例?

时间:2011-02-10 05:07:00

标签: php soap instance

我正在玩SOAP和PHP。但我无法弄清楚为什么返回的对象似乎不保留实例。让我先向您展示示例代码,然后我将解释:

client.php:

  $client = new SoapClient("http://myhost/remote.wsdl");
  try {
    if($client->login("root","toor")) {
        echo $client->getTotal()."\n";
    }
  } catch (SoapFault $exception) {
    echo $exception;
  }

server.php:

class Remote {
  private $auth = false;

  public function login($user, $pass) {
    if($user == "root" && $pass == "toor") {
        $this->auth = true;
        return true;
    } else throw new SoapFault("Server","Access Denied to '$user'.");
  }

  public function getTotal() {
    if($this->auth) {
        return rand(1000,9999);
    } else throw new SoapFault("Server","Error: Not Authorized.");
  }
}

$server = new SoapServer("remote.wsdl");
$server->setClass("Remote");
$server->handle();

我可以“登录”,因此 $ client-> login 的返回值为 true 。 但是,当我拨打 $ client-> getTotal 时, $ this-> auth false (因此会引发错误)。

为了保持我之前设定的值,我需要做什么?

提前谢谢你......

1 个答案:

答案 0 :(得分:0)

确定!解决方案在这里:

http://www.php.net/manual/en/soapserver.setpersistence.php

结果如下:

session_start(); //Important
$server = new SoapServer("remote.wsdl");
$server->setClass("Remote");
$server->setPersistence(SOAP_PERSISTENCE_SESSION); 
$server->handle();

就是这样!