php - Zend_Http_Response 的 getRawBody() 和 getBody() 方法之间的区别?

标签 php http zend-framework response

您好,想知道 $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/

相关文章:

PHP:根据真实语句重定向

python - 如何在 urllib2.URLError 上打印响应正文?

具有 CUDA 功能的 PHP 扩展

php - 如何为整个 Zend Framework 应用程序设置/更改内容类型 header

javascript - 如何使用 Javascript 和 PHP header 重命名正在下载的文件?

javascript - 从php中动态创建的文本框检索值?

php - fopen 在我的服务器上不工作

http - 我应该这样使用 Content-Location header 吗?

http - 为什么我的 Hello World go 服务器被 ApacheBench 压垮了?

php - 我如何在 zend 查询中转换这个多个子查询?