如何将在soap服务器中创建的SESSION转移到soap客户端代码?

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?

dongyuan1970
dongyuan1970 您必须将$client视为忘记了cookie的浏览器,它会记住客户端生命周期中的cookie,但是一旦脚本终止它就会忘记它们。
6 个月之前 回复
doufangyan6862
doufangyan6862 我没有办法测试这个,但我假设客户端使用$SoapClient->_cookies从服务器响应中读取,然后使用$client->__setCookie()在后续的服务器请求中设置cookie
6 个月之前 回复
douba2011
douba2011 我已经阅读过那些但是如果我没有错__setCookie在客户端工作。我需要将在server.php上创建的SESSION数据传输到client.php,我正在做一些非常错误的事情我想因为我还有很多其他问题,比如这些
6 个月之前 回复
dqpdb82600
dqpdb82600 看看stackoverflow.com/questions/13732307/...如何获取有关如何访问cookie数据的任何提示我可以看到php.net/manual/en/soapclient.setcookie.php也存在stackoverflow.com/questions/13388613/...
6 个月之前 回复

1个回答

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;
  }
}
dongtuan8547
dongtuan8547 可以这样想,如果你想为你的用户创建一个presitant状态,由你来决定如何存储$ cookies,因为现在我不知道你在你的应用程序中实际上是如何使用SOAP的。
6 个月之前 回复
duanlin1933
duanlin1933 这取决于您的应用程序设计如何存储肥皂饼干。 在生成$ client的脚本中,您可以将它们存储在会话中,甚至将它们存储在传递给用户浏览器的cookie中。 请记住,使用API​​时我们有两层深度,浏览器cookie和肥皂饼干现在是分开的,直到你用它们做一些事情。
6 个月之前 回复
dqsw7529
dqsw7529 谢谢你的时间,非常感谢。 但是__setCookie是不是重置了client.php中实际创建的cookie? 或者我每次连接时都应该使用它吗? 您能否查看我已经要求的其他2个肥皂问题,以便更好地理解我的问题? 我可能无法说出我的问题导致我的语言能力不足...问题1和问题2
6 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问