doukong1901 2014-11-13 00:30
浏览 42

无法覆盖内容类型的PHP服务器标头

currently the server returns the following headers like so:

2014-11-13 00:20:04.079 myiOSApplication 46715:1003]:

{ status code: 200, headers {
    Connection = close;
    "Content-Length" = 5;
    "Content-Type" = "text/html; charset=UTF-8";
    "Content-Typesss" = "application/json; charset=UTF-8"; //<-- Notice this appears, but soon as I remove the extra s characters, then it wont override the line above. :( so the content always returns `text/html` instead of the desired `application/json`
    Date = "Thu, 13 Nov 2014 00:20:03 GMT";
    Server = "Apache/2.2.26 (Amazon)";
    Status = "200 OK";
    StatusCode = 200;
    "X-Powered-By" = "PHP/5.3.28";
} }

I can set other headers with ease, but as SOON as I try to set a header with the key: Content-Type like so:

header('Content-Type : application/json; charset=UTF-8');

then my key gets overridden with the system one and ignores my header type.

What can I do?

Update 1:

In response to @scuzzy's request for -

What happens if you echo your header then exit(); the script? eg header('Content-Type : application/json; charset=UTF-8');exit('{"hello":"world"}');

Here's the iOS response to that:

2014-11-13 00:59:25.801 myiOSApplication [46849:1f07] RESPONSE: <NSHTTPURLResponse: 0x61800002c060> { URL: http://www.riabp.com/KINGS/Secure/Rajam/Get/Employees } { status code: 200, headers {
    Connection = close;
    "Content-Length" = 18;
    "Content-Type" = "text/html; charset=UTF-8";
    Date = "Thu, 13 Nov 2014 00:59:25 GMT";
    Server = "Apache/2.2.26 (Amazon)";
    "X-Powered-By" = "PHP/5.3.28";
} }
2014-11-13 00:59:25.801 KingsEMS[46849:303] Error: Request failed: unacceptable content-type: text/html
2014-11-13 00:59:25.801 KingsEMS[46849:303] JSON Error:     {"hello":"world"}
  • 写回答

2条回答 默认 最新

  • doulin6761 2014-12-23 15:38
    关注

    I'm not sure if this is still an active question but I've had a similar issue whereby the content-type header was being set by PHP for text/html but when I wanted to download an image, using PHP, I couldn't update the header content-type to image/gif so the file downloaded but couldn't be opened.

    The answer in the end was to return the output buffer and make sure it's empty:

    $html = ob_get_clean();
    $html = str_replace(" ", "", $html);
    

    I am assuming that there was some white space somewhere in the code that was forcing the output to remain as the original content-type.

    Richard

    UPDATE: this should be added before the new header value is set, in case this wasn't obvious.

    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)