![](/img/trans.png)
[英]Using PHP's SoapClient, how to deserialize SOAP responses into a custom class?
[英]How to consume a stateful Web Service using PHP's SoapClient class?
Noob問題。
我正在開發一個使用有狀態Web服務的PHP Web站點。 基本上,我的網站的“控制流程”如下:
我的問題是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();
?>
但它不起作用。 我的代碼出了什么問題?
您將需要使用http://php.net/manual/en/soapclient.getlastresponseheaders.php來查找服務器撤消的“set-cookie”標頭,然后使用http://php.net/manual/en /soapclient.setcookie.php在發送后續請求時設置該cookie。 對不起,無法編寫示例代碼,因為我不知道任何PHP。
要使用有狀態Web服務,您需要在客戶端的SOAP cookie中設置服務器會話的會話ID。 默認情況下,每次發送SOAP請求時,服務器都會生成唯一的會話ID。 為了防止這種情況,只需在SOAP cookie中設置第一個請求獲得的會話ID。 該cookie將與您隨后的所有肥皂調用一起發送。 例如,如果您使用SOAP消費ASP.net Web服務,那么在第一次WS調用之后,獲取響應頭,如下所示:
$client = SoapClient("some.wsdl", array('trace' => 1));
$result = $client->SomeFunction();
$headers = $client->__getLastResponseHeaders();
現在$headers
必須包含名為“ASP.NET_SessionId”的會話ID。 從$headers
獲取ID並創建cookie,如下所示:
//$client->__setCookie($cookieName, $cookieValue);
$client->__setCookie('ASP.NET_SessionId', $cookieValue);
現在,來自客戶端的所有SOAP請求都將包含此會話ID,您的狀態將保留在服務器上。
您還可以通過訪問$ my_soapclient - > _ cookies直接從soap客戶端獲取cookie,因此您不必手動解析響應頭。
請參閱此處: 閱讀HTTP響應標頭中的Set-Cookie指令
但是php手冊中沒有任何內容可以解決這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.