简体   繁体   English

PHP-Zend使会话保持活动状态

[英]PHP - Zend keeping sessions alive

I am making requests from Product 1 to Product 2 . 我正在从产品1产品2提出请求。 3 requests will be sent, the first is authentication via POST where a session will be set on the receiving server, two will be the GET requests to the REST action of choice and three will be the close call to unset the session by POST. 将发送3个请求,第一个是通过POST进行身份验证,其中将在接收服务器上设置会话,两个将是对所选择的REST操作的GET请求,而三个将是通过POST取消会话设置的close调用。

Below is a quick mock up of what is desired: 以下是所需内容的快速模拟:

在此处输入图片说明

I can set the session from request one but when the second GET request is sent I think the Session no longer exists. 我可以根据请求之一设置会话,但是当发送第二个GET请求时,我认为该会话不再存在。 How do I persist that session until the third request to unset it is sent? 如何保持该会话,直到发送第三个取消设置的请求?

Sender: 发件人:

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();
    }
}

Receiver: 接收器:

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();
    }
}

From the client's perspective it's just the cookie(s) that need to be preserved between requests. 从客户端的角度来看,只是在两次请求之间需要保留的cookie。 Take a look at the cookie jar functionality: http://framework.zend.com/manual/1.12/en/zend.http.client.advanced.html#zend.http.client.cookies (particularly example #3). 看一下cookie罐功能: http : //framework.zend.com/manual/1.12/en/zend.http.client.advanced.html#zend.http.client.cookies (特别是示例3)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM