嗨,Wondring$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()针对某些标题进行调整,即解压缩使用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;
}

09-25 17:52