繁体   English   中英

通过在php中设置header()来发送内容类型

[英]content-type wasn send by setting it with header() in php

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

他们没有Content-Type标头

我可能会犯什么错误? 在设置标题之前可能有任何错误吗? -id不执行任何狂杂的事情或其他任何操作-标头的设置没有问题-我在请求和响应之间的模型中唯一更改的标头是http状态,其后是$response-send(); 几乎每次都

渲染树枝模板时可以对标头进行操作吗?

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

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

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

然后我将通过所有验证工具进行绿色测试。 标头已发送。

我可以遍历整个代码,看一下我在哪里松开了标头,但这可能要花很长时间。 有谁遇到过同样的问题,或者可以想象我做错了什么?

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

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

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

忘记了告诉代码何时尝试下一种情况的时间间隔。

我解决了问题。

和...

这是实施的问题。 这是您看不到的东西,因为我没有发布。 确实,这是默认路径,我将客户端希望获得的路径发送为Content-Type。 意思是如果客户端(IE8或验证工具)未发送任何接受标头或我发送的空字符串并且“ 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;
} 

评论的部分是过错。

哦,我的上帝。 ...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM