0

我在通过 php 函数 header(); 设置内容类型时遇到问题;我的完整代码从未发送任何输出。只有响应对象会这样做。在每次输出之前,我都会发送标题。根据 Accept-Header 我设置一个 Content-Type 并进行正确的输出,它以一种简单的方式看起来像这样:

switch($aceptHeader){ 
  case 'text/html': 
  #and some othe stupid acept-headers of the ie
    $this->setHeader('Content-Type: text/html; charset=".."');
    $this->sendHeaders();
    $this->sendDefault();
    break;
  case 'application/json':
    $this->setHeader('Content-Type: application/json');
    $this->sendJSON();
    break;  
  case 'application/xml':
    $this->setHeader('Content-Type: application/xml');
    $this->sendXML();
    break;
}

setHeader 的方法只填充一个数组,它将由 foreach 循环放入 header();功能。这并不重要,因为我直接尝试通过将其更改$this->setHeader()header('Content-Type..');它得到了相同的结果(稍后)输出只是树枝模板对数组的渲染。这是唯一的输出。标题的设置不是问题。当我执行print_r(header_list());. 在那里我确切地看到了我之前设置的标题。我在几乎每个浏览器中都看到相同的响应标头,但并非在每个浏览器中都看到:ie8 在预标记之后仅将 html 代码显示为正文中的字符串。这就是为什么我使用 w3c 验证器(和其他工具)测试我的页面的原因。都显示了相同的结果:

他们没有 Content-Type 标头

我的错误可能是什么?在我设置标题之前会不会有任何错误?- id 不包含任何内容或其他任何东西 - 标头的设置没有任何问题 - 我在请求和响应之间的模型中更改的唯一标头是 http 状态,$response-send();几乎每次都紧随其后。

渲染树枝模板时是否可以对标题进行操作?

问题是:什么会干扰 header() 函数对内容类型标头的设置?

一个有趣的事情是:我将以下代码放在我的 index.php 的开头

header('Content-Type: text/html');
echo 'test';
exit;

然后我得到了所有验证工具的绿色测试。标头已发送。

我可以浏览我的整个代码并查看我在哪里松开了标题,但这可能是一个很长的路要走。有没有人遇到过同样的问题或者可以想象我做错了什么?

这只是一个类似的例子。switch 块不是问题,sendHeader 也不是问题。因为我在整个下午通过直接设置标题在不同的情况下替换了它们 - 每次都具有相同的结果。

我将这些输出开关用于不同的情况,每次都能正常工作。我发送 json,带有一个 jQuery 的 $.ajax() 接受为 json 而不是字符串的标头。我为我的 RESTful api 生成了 xml,它工作正常并以我想要的格式发送数据。只有验证器工具和 ie8 不喜欢它。而且我不明白为什么...

4

2 回答 2

0

我解决了问题。

和...

这是实施的问题。这是你看不到的东西,因为我不发布它。事实上,这是默认路径,我将客户端想要作为 Content-Type 获取的 wath 发送到该路径。表示如果客户端(IE8 或验证工具)未发送任何接受标头或我发送的空字符串和“Content-Type:”,则表示失败。我可以哭了。呸呸呸呸。对此感到抱歉。

为了让您知道我的正确实现(我知道需要一些重构):

    $acceptFormat = $this->request->getAcceptFormat();
#do a switch for the type of the output and send the headers before
switch ($acceptFormat ){
  case 'text/html':
  case 'application/x-www-form-urlencoded':
  case 'application/x-ms-application':
  case 'application/vnd.wap.xhtml+xml':
  case '*/*':
    $this->setHeader('content-type: text/html; charset=UTF-8');
    $this->_sendHeaders();
    $this->_sendDefault();
    break;
  case 'application/json':
    $this->setHeader('Content-Type: application/json');
    $this->_sendHeaders();
    $this->_sendJSON();
    break;
  case 'application/xml':
    $this->setHeader('Content-Type: application/xml');
    $this->_sendHeaders();
    $this->_sendXML();
    break;
  default:
    $this->debugger->log('Unknown Accept-Header set: '.$acceptFormat.' for setting the Content-Type ');
    #$this->setHeader('content-type: '.$acceptFormat);
    $this->setHeader('content-type: text/html; charset=UTF-8');
    $this->_sendHeaders();
    $this->_sendDefault();
    break;
} 

评论部分是错误的。

我的天啊。...

但我永远无法想象,浏览器不会发送任何内容作为接受类型。

于 2013-08-08T06:37:28.277 回答
0
switch($aceptHeader){ 
  case 'text/html': 
  #and some othe stupid acept-headers of the ie
    $this->setHeader('Content-Type: text/html; charset=".."');
    $this->sendHeaders();
    $this->sendDefault();
    break;
  case 'application/json':
    $this->setHeader('Content-Type: application/json');
    $this->sendJSON();
    break;
  case 'application/xml':
    $this->setHeader('Content-Type: application/xml');
    $this->sendXML();
    break;
}

忘记了告诉代码何时尝试下一个案例的中断。

于 2013-08-07T20:11:49.153 回答