PHP-Zend保持会话活动
我从产品1向产品2提出请求。将发送3个请求,第一个是通过POST进行身份验证,其中一个会话将在接收服务器上设置,两个是对所选其余操作的GET请求,三个是通过POST取消会话设置的close调用 下面是所需内容的快速模型: 我可以从请求1设置会话,但当发送第二个GET请求时,我认为会话不再存在。在发送第三个取消设置会话的请求之前,如何保持该会话 发件人:PHP-Zend保持会话活动,php,session,zend-framework,zend-http-client,Php,Session,Zend Framework,Zend Http Client,我从产品1向产品2提出请求。将发送3个请求,第一个是通过POST进行身份验证,其中一个会话将在接收服务器上设置,两个是对所选其余操作的GET请求,三个是通过POST取消会话设置的close调用 下面是所需内容的快速模型: 我可以从请求1设置会话,但当发送第二个GET请求时,我认为会话不再存在。在发送第三个取消设置会话的请求之前,如何保持该会话 发件人: public function sendRestRequest($action, $params= array(), $method = 'G
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();
}
}
从客户端的角度来看,请求之间需要保留的只是cookie。看看cookie jar的功能:(特别是示例#3)。谢谢Tim:)我认为cookie是特定于浏览器的。@NathanDaly cookie确实不是HTTP标准的一部分,而它们只是标题。但是,由于它们非常有用,Zend_Http_客户端(和许多其他客户端)实现了一个更方便的API来模拟cookie。
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();
}
}