How to use stateful web service using PHP class SoapClient?

Question Noob.

I am developing a PHP website that uses a stateful web service. Basically, the “flow of control” of my website is as follows:

  • The user shows the page.
  • The user performs an action.
  • The website server makes a request to the web service using user input as parameters.
  • The web service server completes the request and moves from state A to state B in the process.
  • The website server redirects the user to another page, and we will return to step 1.

My problem is that the website is losing track of the state of the web service between requests. How to get a website to track the status of a web service? I am using the PHP class SoapClient.

I tried serializing the object SoapClientin 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 that will not work. What is wrong with my code?

+5
source share
3 answers

You will need to use http://php.net/manual/en/soapclient.getlastresponseheaders.php to find the set-cookie header saved by the server, and then use http://php.net/manual/en/soapclient. setcookie.php set this cookie on subsequent requests. Sorry, I cannot write sample code, because I do not know PHP.

+2
source

- cookie SOAP . , SOAP, . , , cookie SOAP. cookie . , - ASP.net SOAP, WS :

$client = SoapClient("some.wsdl", array('trace' => 1));
$result = $client->SomeFunction();
$headers = $client->__getLastResponseHeaders();

$headers , "ASP.NET_SessionId". $headers cookie :

//$client->__setCookie($cookieName, $cookieValue);
$client->__setCookie('ASP.NET_SessionId', $cookieValue);

SOAP , .

0

You can also directly get cookies from the soap client by accessing the files $ my_soapclient → _, so you do not need to manually parse the response header.

See here: Reading Set-Cookie instructions in the HTTP response header

But there is nothing in the php manual.

0
source

All Articles