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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器