如何使用PHP的SoapClient类来使用有状态的Web服务?

pyo*_*yon 5 php session web-services soap-client

Noob问题.

我正在开发一个使用有状态Web服务的PHP Web站点.基本上,我的网站的"控制流程"如下:

  1. 向用户显示页面.
  2. 用户执行操作.
  3. Web站点的服务器使用用户输入作为参数向Web Service发出请求.
  4. Web Service的服务器执行请求,并在此过程中从状态A进入状态B.
  5. 网站的服务器将用户重定向到另一个页面,然后我们返回到步骤1.

我的问题是Web站点在请求之间丢失了Web Service的状态.如何使网站跟踪Web服务的状态?我正在使用PHP的标准SoapClient类.

我已经尝试将SoapClient对象序列化为会话变量:

# ws_client.php
<?php
function get_client()
{
    if (!isset($_SESSION['client']))
        $_SESSION['client'] = new SoapClient('http://mydomain/MyWS/MyWS.asmx?WSDL', 'r');
    return $_SESSION['client'];
}
function some_request($input1, $input2)
{
    $client = get_client();
    $params = new stdClass();
    $params['input1'] = $input1;
    $params['input2'] = $input2;
    return $client->SomeRequest($params)->SomeRequestResult;
}
function stateful_request($input)
{
    $client = get_client();
    $params = new stdClass();
    $params['input'] = $input;
    return $client->StatefulRequest($params)->StatefulRequestResult;
}
?>

# page1.php
<?php
session_start();
$_SESSION['A'] = some_request($_POST['input1'], $_POST['input2']);
session_write_close();
header('Location: page2.php');
?>

# page2.php
<?php
session_start();
echo $_SESSION['A']; // works correctly
echo stateful_request($_SESSION['A']); // fails
session_write_close();
?>
Run Code Online (Sandbox Code Playgroud)

但它不起作用.我的代码出了什么问题?

MK.*_*MK. 2

您需要使用 http://php.net/manual/en/soapclient.getlastresponseheaders.php 查找服务器返回的“set-cookie”标头,然后使用 http://php.net/manual/en /soapclient.setcookie.php 在发送后续请求时设置该 cookie。抱歉,无法编写示例代码,因为我不懂 PHP。