I’m playing around with SOAP and PHP. But I can’t figure it out why the returned object seems not to keep the instance. Let me show you first the example code and then I will explain:
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();
I’m able to “login” so the returned value from $client->login is true.
But, when I call $client->getTotal, $this->auth is false (and thus the error is raised).
What do I need to do in order to keep the value I set previously?
Thank you in advance…
Ok! the solution was here:
http://www.php.net/manual/en/soapserver.setpersistence.php
This is the result:
And that’s it!