Magento API 错误:已中止:解析标头时出错:重复标头“Content-Type”;

发布于 2024-11-06 08:33:29 字数 180 浏览 0 评论 0原文

我刚刚将我的服务器升级到 PHP 5.3 和 Fast CGI。不幸的是,这导致magento api返回一个奇怪的错误

中止:错误解析标头:重复标头'Content-Type'

我已经尝试了Magento论坛的各种建议,但无济于事。

我正在运行 1.4.0.1。关于如何解决这个问题有什么建议吗?

I just upgraded my 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

他是夢罘是命 2024-11-13 08:33:29

来自 http://www.magentocommerce.com/boards/v/viewthread/229253/< /a>

编辑

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

使用以下代码 并替换 sendHeaders() 函数
(你不应该覆盖核心类,但这会让它工作。使用 app/code/local/Mage/... 不起作用,因为它是一个控制器)

/**
 * 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();
}

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();
}
時窥 2024-11-13 08:33:29

您使用什么网络服务器?

检查您的 Apache/nginx - fcgi 是否配置为默认发送此标头。

What webserver are you using?

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

苍暮颜 2024-11-13 08:33:29

试试这个 - 按名称删除

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

并添加您的标题

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

Try this - remove by name

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

and add your header

$this->getResponse()->setHeader('Content-Type', 'application/json');
层林尽染 2024-11-13 08:33:29

我见过一些 Magento 和 fcgi 出现问题的网站。通常是因为 fast cgi 默认配置为发送标头。不过 mod_php 不会出现这个问题。大多数时候我都遇到过 Ajax 请求的问题。

我最终修复了 json 响应:

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

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

添加参数 true 将确保 setHeader() 方法搜索标头数组并取消设置之前具有相同名称的任何标头它设置第二个标头。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文