duanqi6274 2019-08-12 10:29
浏览 157

如何将在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?

  • 写回答

1条回答

  • dongqi0644 2019-08-13 04:46
    关注

    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;
      }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配