我正在用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>
由于 Location Header 的存在,PHP 会自动添加一些 HTML 代码和 HTML 内容类型到响应中。
因此,我的 API 将无法与其客户端正常工作。
编辑: IIS 7.5 Windows 7 专业版。
http://example.com/dir
而不是http://example.com/dir/
,Apache会抱怨并输出类似的HTML:IIS也可能会这样做。 - Waleed Khan