Noob question.
I am developing a PHP Web site that consumes a stateful Web Service. Basically, the "flow of control" of my Web site is the following one:
- The user is shown a page.
- The user performs an action.
- The Web site's server does a request to the Web Service, using user input as parameters.
- The Web Service's server executes the request, and goes from state A to state B in the process.
- The Web site's server redirects the user to another page, and we go back to step 1.
My problem is that the Web site loses track of the Web Service's state between requests. How do I make the Web Site keep track of the Web Service's state? I am using PHP's standard SoapClient
class.
I have tried serializing the SoapClient
object into a session variable:
# 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();
?>
But it doesn't work. What's wrong with my code?