您好,想知道 $response->getBody() 和 $response->getRawBody() 之间的区别是什么;
$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();
}
最佳答案
getRawBody()
按原样返回 http 响应的正文。
getBody()
针对某些 header 进行调整,即解压缩使用 gzip 发送的内容或缩小内容编码 header 。或者分块的传输编码 header 。
解决这些问题的最简单方法是查看代码。也是一次很棒的学习经历。为简洁起见,对代码进行了编辑。
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;
}
关于php - Zend_Http_Response 的 getRawBody() 和 getBody() 方法之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5125497/