1
我正在請求從產品1到產品2。將發送3個請求,第一個是通過POST進行身份驗證,其中會話將在接收服務器上設置,其中兩個將成爲對所選REST操作的GET請求,另外三個將是通過POST取消設置會話的關閉調用。PHP - Zend保持會話存活
下面是一個快速模仿的了什麼希望:
我可以從一個要求設置的會議,但被送到第二GET請求時,我認爲會話不再存在。我如何堅持這個會話,直到第三個請求解除它被髮送?
發件人:
public function sendRestRequest($action, $params= array(), $method = 'GET')
{
try {
// Authenticate request
$this->sendAuthenticateRequest();
$this->client->setUri('https://product1/controller/'.$action);
// Set post parameter to the authentication token
$this->setRequestParams($params, false, 'GET');
// Make request
$restRequest = $this->client->request($method);
// Get response
$restResponse = $restRequest->getBody();
$this->debugging = 0;
if ($this->debugging == 1) {
echo '<b>Rest Request:</b>';
echo '<pre>';
echo $restRequest;
echo '<pre>';
echo '<b>Rest Response:</b>';
echo '<pre>';
echo $restResponse;
echo '<pre>';
}
// Check the request was successful
if ($restRequest->getStatus() != 200) {
throw new Zend_Exception('The request returned a '.$restRequest->getStatus().' status code');
}
// Clear parameters so another request can be sent
$this->client->resetParameters();
// Close request
$this->closeRequest();
return $restResponse;
} catch (Zend_Exception $e) {
$this->setError(500, $e->getMessage());
$this->generateXml();
}
}
接收機:
public function authenticationIn($passPhrase)
{
try {
if (!isset($passPhrase)) {
throw new Zend_Rest_Exception('No authentication key is detected.');
}
// Construct pass key from pass phrase and the shared secret
$generatedKey = $this->generateApiKey($passPhrase);
// Get KEY setting value from global_settings
$storedKey = $this->getQuidApiKey();
if ($generatedKey != $storedKey) {
// Bad PASS_PHRASE key send a failed authenticate response
header('HTTP/1.1 500 Application Error');
$authObject = new \stdClass();
$authObject->success = false;
echo json_encode($authObject);
exit;
} else {
// This session needs to persist until the 3rd request to unset it
$_SESSION['auth'] = true;
return true;
}
} catch (Zend_Service_Exception $e) {
$this->setError(500, $e->getMessage());
$this->generateXml();
}
}
謝謝蒂姆:)我認爲Cookies是瀏覽器特定的。 – Kal
@NathanDaly Cookie確實不是HTTP標準的一部分 - 反過來它們只是標題。但是,由於它們非常有用,Zend_Http_Client(和許多其他客戶端)實現了更方便的API來模擬Cookie。 – shevron