简体   繁体   English

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

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

I have a problem with setting the content-type by the php function header(); 我在通过php函数header()设置内容类型时遇到问题; My complete code doesn't ever send any output. 我完整的代码永远不会发送任何输出。 Only the response-object does it. 只有响应对象可以做到。 Before every output i send the headers. 在每个输出之前,我发送标题。 Depending on the Accept-Header i set a Content-Type and do the right output, it in an easy way it looks like that: 根据我的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;
}

the method for the setHeader fills only an array, wicht will be put by an foreach-loop into the header(); setHeader的方法仅填充一个数组,将通过foreach循环将wicht放入header()中; function. 功能。 This is not important, cause i tried it in the direct way by changing it $this->setHeader() to header('Content-Type..'); 这并不重要,因为我通过直接将$this->setHeader()更改为header('Content-Type..');来进行$this->setHeader() header('Content-Type..'); it has got the same result (later) The output ist just an rendering of an array by an twig template. 它具有相同的结果(稍后)输出仅是由树枝模板渲染数组的结果。 This is the only output. 这是唯一的输出。 The setting of the headers is not the problem. 标头的设置不是问题。 I even get a positive result, when i do a print_r(header_list()); 当我执行print_r(header_list());时,我什至得到正面的结果print_r(header_list()); . There i see exactly that header i set before. 在那里,我确切地看到了我之前设置的标头。 I see the same response header in almost every browser, but not in every: the ie8 shows me only the html-code as a string in the body after a pre-tag. 我在几乎所有浏览器中都看到相同的响应标头,但在每种浏览器中却看不到:ie8在预标记后的正文中仅向我显示html代码作为字符串。 This was the reason, why i tested my page by the w3c validator (and other tools). 这就是为什么我用w3c验证器(和其他工具)测试页面的原因。 All showed the same result: 全部显示相同的结果:

they get no Content-Type header 他们没有Content-Type标头

what could be my mistake? 我可能会犯什么错误? Could there be any mistake before i set the header? 在设置标题之前可能有任何错误吗? - id do no wild inlcude stuff or anything else - the setting of the header make no trouble - the only headers i change in the models between request and response is the http status, wich is followed by an $response-send(); -id不执行任何狂杂的事情或其他任何操作-标头的设置没有问题-我在请求和响应之间的模型中唯一更改的标头是http状态,其后是$response-send(); in almost every time. 几乎每次都

Could there be a manipulation of the header while rendering the twig-template? 渲染树枝模板时可以对标头进行操作吗?

The Question is: What could disturb the setting of the content-type header by the header() function? 问题是:什么会干扰header()函数对内容类型标头的设置?

An interesting thing ist: i put following code to the begining of my index.php 一个有趣的事:我将以下代码放在index.php的开头

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

and than i get green test by all validation tool. 然后我将通过所有验证工具进行绿色测试。 The header was send. 标头已发送。

I could go throught my whole code and look where i loose the header, but this could be a long long way. 我可以遍历整个代码,看一下我在哪里松开了标头,但这可能要花很长时间。 Is there anybody that has had the same problem or could imagine what i have done wrong? 有谁遇到过同样的问题,或者可以想象我做错了什么?

This is only an look-a-like example. 这只是一个类似的示例。 The switch block is not the problem, the sendHeader not, too. 开关块不是问题,sendHeader也不是。 Cause i replaced them in different cases the whole afternoon by setting the header directly - every time with the same result. 因为我整个下午都在不同情况下通过直接设置标题来替换它们-每次都具有相同的结果。

I used these output switch for different cases and it works fine every time. 我在不同情况下使用了这些输出开关,每次都能正常工作。 I send json, with an header that $.ajax() of jQuery accepts as an json and not as an string. 我发送json,并带有jQuery的$ .ajax()接受的标头作为json而不是字符串。 I produced xml for my RESTful api, that works fine and sends the data in that format i wanted it. 我为我的RESTful API生产了xml,它可以正常工作,并以我想要的格式发送数据。 Only the validator tool and the ie8 doesn't like it. 仅验证器工具和ie8不喜欢它。 And I don't understand why... 我不明白为什么...

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

Forgot the breaks in which tell the code when to try the next case. 忘记了告诉代码何时尝试下一种情况的时间间隔。

I solved the Problem. 我解决了问题。

And... 和...

it was a problem of the implementation. 这是实施的问题。 It was a thing you can't see, cause i don't post it. 这是您看不到的东西,因为我没有发布。 Indeed it was the default path, where i send that wath the client wants to get as Content-Type. 确实,这是默认路径,我将客户端希望获得的路径发送为Content-Type。 Mean if the client (IE8 or validation tools) sends no accept-header or an empty string i send and "Content-Type: " this is fail. 意思是如果客户端(IE8或验证工具)未发送任何接受标头或我发送的空字符串并且“ Content-Type:”,则失败。 I could cry. 我会哭。 grrrrrrrrrr. grrrrrrrrrr。 Sorry for that. 抱歉

To let you know my right implementation (needs some refactoring i know): 为了让您知道我的正确实现(我需要一些重构):

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

the commented part was the fault. 评论的部分是过错。

oh my god. 哦,我的上帝。 ... ...

but i could never imagine, that a browser would ever send nothing as accept-type. 但我无法想象,浏览器将不会发送任何内容作为接受类型。

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

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