2011-02-26 32 views
1

嗨你知道$ response-> getBody()和$ response-> getRawBody()之間的區別是什麼?Zend_Http_Response的getRawBody()和getBody()方法之間的區別?

$this->_client->setUri('http://www.google.com/ig?hl=en'); 
     try { 
      $response = $this->_client->request(); 
     }catch(Zend_Http_Exception $e) 
     { 
      echo 'Zend http Client Failed'; 
     } 
     echo get_class($response); 
     if($response->isSuccessful()) 
     { 
      $response->getBody(); 
      $response->getRawBody(); 

     } 

回答

8

getRawBody()原樣返回http響應的主體。

getBody()調整某些標題,即解壓縮使用gzip或縮小內容編碼標頭髮送的內容。或分塊傳輸編碼頭。

最簡單的方法是將這些問題簡單地看作代碼。也是一個很好的學習經驗。代碼編輯簡潔。

public function getRawBody() 
{ 
    return $this->body; 
} 

public function getBody() 
{ 
    $body = ''; 

    // Decode the body if it was transfer-encoded 
    switch (strtolower($this->getHeader('transfer-encoding'))) { 
     case 'chunked': 
      // Handle chunked body 
      break; 
     // No transfer encoding, or unknown encoding extension: 
     default: 
      // return body as is 
      break; 
    } 

    // Decode any content-encoding (gzip or deflate) if needed 
    switch (strtolower($this->getHeader('content-encoding'))) { 
     case 'gzip': 
      // Handle gzip encoding 
      break; 
     case 'deflate': 
      // Handle deflate encoding 
      break; 
     default: 
      break; 
    } 

    return $body; 
} 
+0

我現在明白了差異,謝謝花時間回答, – 2011-02-26 07:43:43

1

HTTP正文可能以各種方式編碼。 )

http://en.wikipedia.org/wiki/Chunked_transfer_encoding

getBody(將返回一個處理HTTP主體,根據它的編碼類型: 例如,它可以以不同的塊分割,每一個前面有一個塊大小,或gzip壓縮。 getRawBody將按原樣返回HTTP主體。

+0

感謝您花時間回答。 – 2011-02-26 07:42:58