Zend Framework和响应gzip压缩

时间:2011-06-01 09:38:37

标签: zend-framework gzip

我正在寻找一种方法来gzip我的XML响应,只有它们。我没有在Zend Framework中找到任何材料如何做到这一点。我的抽象控制器中有一个响应方法,如下所示:

public function xmlResponse(SimpleXMLElement $xml, $contentType = null){
    $this->_helper->layout->disableLayout();
    Zend_Controller_Front::getInstance()->setParam('noViewRenderer', true);

    $this->_response->setHeader('Content-Type', ($contentType) ? $contentType : 'text/xml');
    $this->_response->setBody($xml->asXML());
} // xmlResponse

我想在这里添加gzip压缩。

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

这是你在找什么?:

$this->_response->setHeader('Content-Type', 'application/x-gzip');
$filter     = new Zend_Filter_Compress('Gz');
$compressed = $filter->filter($xml->asXML());
$this->_response->setBody($compressed);

编辑:您可以试试这个,我还没有测试过它:

$this->_response->setHeader("Accept-Encoding", "gzip, deflate");  
$this->_response->setHeader("X-Compression", "gzip");  
$this->_response->setHeader("Content-Encoding", "gzip");  
$this->_response->setHeader("Content-type", "text/xml");  
$this->_response->setHeader("Cache-Control", "must-revalidate");
$filter = new Zend_Filter_Compress('Gz');
$compressed = $filter->filter($xml->asXML());
$this->_response->setBody($compressed);
$this->_response->sendResponse();

编辑:或者您可以将此行添加到.htaccess文件中

AddOutputFilterByType DEFLATE text/xml

我希望这会有所帮助

亲切的问候,

加里

答案 1 :(得分:0)

如果您使用 nginx 服务器,请在/etc/nginx/nginx.conf取消注释以下内容。在其中添加 text / xml !这是我的配置。

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 9;
    gzip_buffers 16 8k;
    gzip_http_version 1.0;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

然后通过sudo /etc/init.d/nginx restart重启。

相关问题