W7 Pro IIS 7.5覆盖PHP位置:标题 [英] W7 Pro IIS 7.5 overwrites PHP Location: Header

查看:82
本文介绍了W7 Pro IIS 7.5覆盖PHP位置:标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中创建RESTful API,但遇到了问题。
当客户端将数据发布到服务器时,服务器应该返回:

 状态代码201 CREATED 
具有新对象位置的标题位置
内容类型应用程序/ 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创建
内容类型:text / html; charset = UTF-8
位置:http://google.no/
服务器:Microsoft-IIS / 7.5
X-Powered-By:PHP / 5.4.5
X -Powered-By:ASP.NET
日期:2012年8月22日星期三13:52:57 GMT
内容长度:209

< head>< title>文件已移动< / title>< / head>
< body>< h1> Object Moved< / h1>可以找到此文档< a HREF =http://google.no/> here< / a>< / body> <?xml version =1.0encoding =utf-8standalone =yes?>
< Jada>< / Jada>

由于位置标题,PHP会自动将一些HTML代码和HTML内容类型添加到响应中。



因此,我的api将无法与其客户合作。



编辑:
IIS 7.5 Windows 7 Proffesional

解决方案

解决方案是创建一个IIS模块,将标题Custom-Location重写为FastCGI完成后的位置。



然后,FastCGI根本不会知道我们正在发送一个Location标头,它不会修改我的响应。 / p>

模块:

  string Location = context.Response.Headers [ Custom-Location]为字符串; 
if(!string.IsNullOrEmpty(Location))
{
context.Response.Headers.Remove(Custom-Location);
context.Response.AddHeader(Location,Location);
}

PHP:

  header(Custom-Location:http://google.no,true,201); 
header(Content-Type:application / xml);
echo< xml>< / xml>;

(仍为虚拟代码,不是极其正确的代码:))


I am creating a RESTful API in PHP, and have encountered a problem. When the client is posting data to the server, the server should return:

Status code 201 CREATED
Header Location with the location of the new object
Content-Type application/xml

<SomeXmlData></SomeXmlData>

Dummy code, producing the problem on my computer:

<?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>";
?>

The HTTP result is

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 automatically adds some HTML code, and HTML content-type to the response, because of the location header.

Because of this, my api won't work with its clients.

EDIT: IIS 7.5 Windows 7 Proffesional

解决方案

The solution was to create an IIS module, that rewrites the header "Custom-Location" to "Location" after FastCGI is done.

Then, FastCGI won't know that we are sending a Location header at all, and it won't modify my response.

The module:

string Location = context.Response.Headers["Custom-Location"] as string;
if (!string.IsNullOrEmpty(Location))
{
    context.Response.Headers.Remove("Custom-Location");
    context.Response.AddHeader("Location", Location);
}

PHP:

header("Custom-Location: http://google.no",true,201);
header("Content-Type: application/xml");
echo "<xml></xml>";

(still dummy code, not extremly correct code :) )

这篇关于W7 Pro IIS 7.5覆盖PHP位置:标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆