开发者

Magento API Error: aborted: error parsing headers: duplicate header 'Content-Type'

开发者 https://www.devze.com 2023-03-05 00:19 出处:网络
I just upgraded my 开发者_C百科server to PHP 5.3 and Fast CGI.Unfortunately that caused the magento api to return a strange error

I just upgraded my 开发者_C百科server to PHP 5.3 and Fast CGI. Unfortunately that caused the magento api to return a strange error

aborted: error parsing headers: duplicate header 'Content-Type'

I've tried various suggestions from the Magento fourms to no avail.

I'm running 1.4.0.1. Any suggestions to how to reconcile this problem?


From http://www.magentocommerce.com/boards/v/viewthread/229253/

Edit

app/code/core/Mage/Core/Controller/Response/Http.php

and replace the sendHeaders() function with the following code (You're not supposed to override core classes, but this will get it working. Using app/code/local/Mage/... doesn't work because it is a controller)

/**
 * Transport object for observers to perform
 *
 * @var Varien_Object
 */
protected static $_transportObject = null;

public function sendHeaders()
{
    if (!$this->canSendHeaders()) {
        Mage::log('HEADERS ALREADY SENT: '.mageDebugBacktrace(true, true, true));
        return $this;
    }

    if (in_array(substr(php_sapi_name(), 0, 3), array('cgi', 'fpm'))) {
        // remove duplicate headers
        $remove = array('status', 'content-type');

        // already sent headers
        $sent = array();
        foreach (headers_list() as $header) {
            // parse name
            if (!$pos = strpos($header, ':')) {
                continue;
            }
            $sent[strtolower(substr($header, 0, $pos))] = true;
        }

        // raw headers
        $headersRaw = array();
        foreach ($this->_headersRaw as $i=>$header) {
            // parse name
            if (!$pos = strpos($header, ':'))
                    continue;
            $name = strtolower(substr($header, 0, $pos));

            if (in_array($name, $remove)) {
                // check sent headers
                if (isset($sent[$name]) && $sent[$name]) {
                    unset($this->_headersRaw[$i]);
                    continue;
                }

                // check header
                if (isset($headers[$name]) && !is_null($existing = $headers[$name])) {
                    $this->_headersRaw[$existing] = $header;
                    unset($this->_headersRaw[$i]);
                } else {
                    $headersRaw[$name] = $i;
                }
            }
        }

        // object headers
        $headers = array();
        foreach ($this->_headers as $i=>$header) {
            $name = strtolower($header['name']);
            if (in_array($name, $remove)) {
                // check sent headers
                if (isset($sent[$name]) && $sent[$name]) {
                    unset($this->_headers[$i]);
                    continue;
                }

                // check header
                if (isset($headers[$name]) && !is_null($existing = $headers[$name])) {
                    $this->_headers[$existing] = $header;
                    unset($this->_headers[$i]);
                } else {
                    $headers[$name] = $i;
                }

                // check raw headers
                if (isset($headersRaw[$name]) && !is_null($existing = $headersRaw[$name])) {
                    unset($this->_headersRaw[$existing]);
                }
            }
        }
    }

    parent::sendHeaders();
}


What webserver are you using?

Check if your Apache/nginx - fcgi is configured to send this header by default.


Try this - remove by name

$this->getResponse()->clearHeader('Content-Type');

and add your header

$this->getResponse()->setHeader('Content-Type', 'application/json');


I've seen a few sites which have troubles with Magento and fcgi. Usually because fast cgi is configured to send a header by default. The problem doesn't occur with mod_php though. Most of the time I've experienced the issue with an Ajax request.

I ended up fixing the json response:

From

 $this->getResponse()->setHeader('Content-type', 'application/json');

To

$this->getResponse()->setHeader('Content-type', true, 'application/json');

Adding the parameter true will make sure the setHeader() method searches the array of headers and unset any headers with the same name before it sets a second header.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号