Can';t重写内容类型PHP服务器头
当前,服务器返回如下标头,如下所示: 2014-11-13 00:20:04.079 Myios申请46715:1003]:Can';t重写内容类型PHP服务器头,php,header,http-headers,content-type,server,Php,Header,Http Headers,Content Type,Server,当前,服务器返回如下标头,如下所示: 2014-11-13 00:20:04.079 Myios申请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";
{ 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";
} }
然后,我的密钥被system one覆盖,并忽略我的头类型
我能做什么
更新1:
回应@scuzzy的以下请求:
如果回显标题然后退出()会发生什么;剧本?例如header('Content-Type:application/json;charset=UTF-8');退出({“hello”:“world}”) 以下是iOS对此的回应:
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"}
2014-11-13 00:59:25.801 myiOSApplication[46849:1f07]响应:{URL:http://www.riabp.com/KINGS/Secure/Rajam/Get/Employees }{状态代码:200,标题{
连接=关闭;
“内容长度”=18;
“内容类型”=“文本/html;字符集=UTF-8”;
日期=“2014年11月13日星期四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]错误:请求失败:不可接受的内容类型:text/html
2014-11-13 00:59:25.801 KingsEMS[46849:303]JSON错误:{“你好”:“世界”}
我不确定这是否仍然是一个活跃的问题,但我遇到了一个类似的问题,即内容类型标题由PHP设置为text/html,但当我想使用PHP下载图像时,我无法将标题内容类型更新为image/gif,因此文件已下载但无法打开
最后的答案是返回输出缓冲区并确保其为空:
$html = ob_get_clean();
$html = str_replace(" ", "", $html);
我假设代码中的某个地方有一些空白,迫使输出保持为原始内容类型
理查德
更新:这应该在设置新的头值之前添加,以防这不明显。如果您使用的是Silex或Zend等框架,或者只是编写普通的php代码,并且正在使用这些函数
json_encode($query) //php json function
$app->json($query) //Silex framework function`
但这些都不能解决您接收json内容类型的需要,有一个小技巧可以帮助您:
header('Content-Type: application/json'); //before everything
$query =.... //retrieve your data in every way you need
$queryjsoned=json_encode($query); //jsonify your query
exit($json); //it's very important,it solves your life
return $queryjsoned; //if you are in a function/Route,but it isn't mandatory
非常奇怪的是,您需要执行退出来覆盖内容类型。如果您回显您的标题,然后
exit()代码>脚本?例如header('Content-Type:application/json;charset=UTF-8');退出({“hello”:“world}”)
@Scuzzy你能检查一下更新吗?抱歉@Pavan我没有足够的知识来帮助你做进一步的测试,那么让我做你的小测试有什么意义呢?是的,这仍然是一个活跃的问题。发现2:如果查询是错误的,并且你用json编码它,json序列化函数会给你一个错误并返回纯文本html。
header('Content-Type: application/json'); //before everything
$query =.... //retrieve your data in every way you need
$queryjsoned=json_encode($query); //jsonify your query
exit($json); //it's very important,it solves your life
return $queryjsoned; //if you are in a function/Route,but it isn't mandatory