简体   繁体   中英

How to transfer SESSION created in soap server to soap client code?

I've created a non-wsdl soap server with PHP to run functions from all servers I own. There are bunch of problems on this as you can see from my profile but this I hope is solvable. I cannot transfer SESSION data between server and client.

Already used

$server->setPersistence(SOAP_PERSISTENCE_SESSION);

and set session_id manually

session_id ('ID');
session_start ();

but no luck to transfer SESSION data to client.

Is there a way to transfer SESSION data created on soap-server.php to soap-client.php?

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
  1. 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.
  2. The second client is independent and has no knowledge of the existing session and fails as expected.
  3. 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;
  }
}

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