Zend框架和响应gzip压缩



我正在寻找一种方法来gzip我的XML响应,并且只对它们进行gzip。我没有在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压缩。

谢谢你的帮助!

这就是您想要的吗?:

$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

我希望这能帮助

问候,

Garry

如果使用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重新启动。

最新更新