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

发布于 2024-10-19 02:40:04 字数 512 浏览 3 评论 0原文

您好,想知道 $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();

        }

Hi wondring whats the difference between $response->getBody() and $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();

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清醇 2024-10-26 02:40:04

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;
}

getRawBody() returns the body of the http response as is.

getBody() adjust for certain headers i.e. decompresses content sent with gzip or deflate content-encoding headers. Or the chunked transfer-encoding header.

The simplest way figure these questions out in to simply look at the code. Also a great learning experience. Code is edited for brevity.

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;
}
未蓝澄海的烟 2024-10-26 02:40:04

HTTP 主体可以以多种方式编码。
例如,它可以被分割成不同的块,每个块前面都有块大小,或者压缩:

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

getBody() 将根据其编码类型返回已处理的 HTTP 正文。
getRawBody 将按原样返回 HTTP 正文。

The HTTP body may be encoded in various ways.
For example, it may be split in different chunks, each one preceded with the chunk size, or gzipped:

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

getBody() will return a processed HTTP body, according to it's encoding type.
getRawBody will return the HTTP body as is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文