我正在从产品 1向产品 2提出请求提出请求。将发送 3 个请求,第一个是通过 POST 进行的身份验证,其中将在接收服务器上设置一个会话,两个是对所选 REST 操作的 GET 请求,三个是通过 POST 取消设置会话的关闭调用。
以下是所需内容的快速模型:
我可以从请求一中设置会话,但是当发送第二个 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();
}
}