Zend Framework and response gzip compression
I'm looking for a way to gzip my XML responses and only them. I didn't find any materials how to do this in Zend Framework. I have a response method in my abstract controller, like this:
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
and I want to add gzip compression in here.
Thank开发者_开发百科s for help!
Is this what you are looking for?:
$this->_response->setHeader('Content-Type', 'application/x-gzip');
$filter = new Zend_Filter_Compress('Gz');
$compressed = $filter->filter($xml->asXML());
$this->_response->setBody($compressed);
EDIT: You could try this, I have not tested it though:
$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();
EDIT: Or you could just add this line to your .htaccess file
AddOutputFilterByType DEFLATE text/xml
I hope this helps
Kind regards,
Garry
If you use nginx server, just uncomment the following at your /etc/nginx/nginx.conf
. Add text/xml is in it! That's my config.
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;
Then restart it via sudo /etc/init.d/nginx restart
.
精彩评论