【发布时间】:2024-04-28 12:50:01
【问题描述】:
我正在用 PHP 创建一个 RESTful API,但遇到了一个问题。 当客户端向服务器发送数据时,服务器应该返回:
Status code 201 CREATED
Header Location with the location of the new object
Content-Type application/xml
<SomeXmlData></SomeXmlData>
虚拟代码,在我的电脑上产生问题:
<?php
header("Location: http://google.no/",true,201);
header("Content-Type: application/xml;charset=iso-8859-1");
echo "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n";
echo "<Jada></Jada>";
?>
HTTP 结果是
HTTP/1.1 201 Created
Content-Type: text/html; charset=UTF-8
Location: http://google.no/
Server: Microsoft-IIS/7.5
X-Powered-By: PHP/5.4.5
X-Powered-By: ASP.NET
Date: Wed, 22 Aug 2012 13:52:57 GMT
Content-Length: 209
<head><title>Document Moved</title></head>
<body><h1>Object Moved</h1>This document may be found <a HREF="http://google.no/">here</a></body><?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Jada></Jada>
由于位置标头,PHP 会自动添加一些 HTML 代码和 HTML 内容类型到响应中。
因此,我的 api 无法与它的客户端一起使用。
编辑: IIS 7.5 Windows 7 专业版
【问题讨论】:
-
为什么您的内容类型标头和 XML 声明在字符编码方面存在分歧?
-
我认为这是你的服务器在做的,而不是 PHP。
-
X-Powered-By: PHP/5.4.5 X-Powered-By: ASP.NET?每次看到与 IIS 相关的东西,都想尖叫着跑过去。 -
那肯定是 不是 原始 PHP 这样做的。需要更好的诊断和/或更多信息才能回答。
-
在我的本地服务器上尝试使用 Apache:发送
Location标头不会生成自动 HTML。但是,应该注意,如果您使用http://example.com/dir而不是http://example.com/dir/并输出类似的 HTML,Apache 会报错:IIS 也可能会这样做。
标签: php rest iis http-headers