PHP - Zend保持会话活着

时间:2014-02-10 10:37:58

标签: php session zend-framework zend-http-client

我正在向产品1 产品2 提出请求。将发送3个请求,第一个是通过POST进行身份验证,其中将在接收服务器上设置会话,两个将是对所选REST操作的GET请求,三个将是通过POST取消设置会话的关闭调用。 / p>

以下是对所需内容的快速模拟:

enter image description here

我可以从请求1设置会话,但是当发送第二个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();
    }
}

1 个答案:

答案 0 :(得分:1)

从客户端的角度来看,它只是需要在请求之间保留的cookie。看看cookie jar功能:http://framework.zend.com/manual/1.12/en/zend.http.client.advanced.html#zend.http.client.cookies(特别是示例#3)。