通过在php中设置header()来发送content-type wasn

时间:2013-08-07 20:08:12

标签: php http header response content-type

我有一个问题,通过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的方法只填充一个数组,wicht将由foreach-loop放入header();功能。这并不重要,因为我通过将其$this->setHeader()更改为header('Content-Type..');以直接的方式尝试了它,它得到了相同的结果(稍后) 输出只是通过树枝模板渲染数组。这是唯一的输出。标题的设置不是问题。当我做print_r(header_list());时,我甚至得到了积极的结果。在那里,我看到我之前设置的标题。 我几乎在每个浏览器中看到相同的响应标头,但不是每个浏览器都有: ie8只显示html代码作为预标记后的正文中的字符串。这就是为什么我用w3c验证器(以及其他工具)测试我的页面的原因。所有都显示了相同的结果:

他们没有内容类型标题

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

渲染twig-template时是否会对标题进行操作?

问题是:什么可能会扰乱header()函数设置内容类型标题?

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

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

并且我得到了所有验证工具的绿色测试。标题已发送。

我可以查看整个代码并查看我松散标题的位置,但这可能需要很长时间。有没有人遇到过同样的问题或想象我做错了什么?

这只是一个看起来很像的例子。交换机块不是问题,sendHeader也不是问题。因为我在整个下午通过直接设置标题来替换它们 - 每次都有相同的结果。

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

2 个答案:

答案 0 :(得分: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;
}

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

答案 1 :(得分:0)

我解决了问题。

和...

这是一个实施问题。这是你看不到的东西,因为我不发布它。 事实上,这是默认路径,我发送客户希望获得内容类型的wath。意味着如果客户端(IE8或验证工具)没有发送accept-header或者我发送的空字符串和“Content-Type:”,则表示失败。我可以哭grrrrrrrrrr。对不起。

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

    $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;
} 

评论部分是错误。

哦,天啊。 ......

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

相关问题