2014-02-10 116 views
1

我正在請求從產品1產品2。將發送3個請求,第一個是通過POST進行身份驗證,其中會話將在接收服務器上設置,其中兩個將成爲對所選REST操作的GET請求,另外三個將是通過POST取消設置會話的關閉調用。PHP - Zend保持會話存活

下面是一個快速模仿的了什麼希望:

enter image description here

我可以從一個要求設置的會議,但被送到第二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

從客戶的角度來看,它只是需要請求之間要保留該cookie(一個或多個)。看一下餅乾罐的功能:http://framework.zend.com/manual/1.12/en/zend.http.client.advanced.html#zend.http.client.cookies(特別是示例#3)。

+0

謝謝蒂姆:)我認爲Cookies是瀏覽器特定的。 – Kal

+1

@NathanDaly Cookie確實不是HTTP標準的一部分 - 反過來它們只是標題。但是,由於它們非常有用,Zend_Http_Client(和許多其他客戶端)實現了更方便的API來模擬Cookie。 – shevron