Given this soap server
class MyClass
{
public function __construct(){
session_start();
}
public function login( $user )
{
$_SESSION['user'] = $user;
return true;
}
public function getUserName()
{
return isset( $_SESSION['user'] ) ? $_SESSION['user'] : false;
}
}
$server = new SoapServer( null, array( 'uri' => 'http://localhost/scratch/soap.server.php' ) );
$server->setClass('MyClass');
$server->handle();
And this soap client
$url = 'http://localhost/scratch/soap.server.php';
$config = array( 'location' => $url, 'uri' => $url );
// Call the "login" function to set the user name
$firstClient = new SoapClient(null, $config);
$firstClient->login( array( 'MyUserName' ) ); // ONLY CALL LOGIN ONCE
var_dump( $firstClient->getUserName() ); // TRUE
// Track the cookies
$cookies = $firstClient->__getCookies();
// Second Client fails because we didn't set cookies
$secondClient = new SoapClient(null, $config);
var_dump( $secondClient->getUserName() ); // FALSE
// Works because we've set cookies from the first request
$thirdClient = new SoapClient(null, $config );
$thirdClient->__setCookie( 'PHPSESSID', $cookies['PHPSESSID'][0] );
var_dump( $thirdClient->getUserName() ); // TRUE
- You can see I am creating three seperate soap clients, the first performs the login, you can see that the subsequent
getUserName()
work because we're using the same connection context and its re-using the initial cookies internally.
- The second client is independent and has no knowledge of the existing session and fails as expected.
- the third client injects the cookie from the first client, and is able to track the username through the session and
getUserName()
is able to resume without the login()
function.
Ideally you would automate the injection of the cookie programmatically instead of my "hard coding" for the purpose of this test
So from here, you have to manage the cookies for the soap client yourself.
If you want the session data itself, there is nothing stopping you from creating an exporting function... eg getSessionData
in this example
class MyServerClass
{
public function __construct(){
session_start();
}
public function login( $user )
{
$_SESSION['user'] = $user;
$_SESSION['SomeObject'] = new stdClass();
$_SESSION['SomeObject']->foo = 'bar';
return true;
}
public function getSessionData()
{
return $_SESSION;
}
}