更改 php 中输出的 MIME 类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/152006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:42:01  来源:igfitidea点击:

change mime type of output in php

phpxmlmime-types

提问by Sam

I've got a php script. Most of the time the script returns html, which is working fine, but on one occasion (parameter ?Format=XML) the script returns XML instead of HTML.

我有一个 php 脚本。大多数情况下,脚本返回 html,这工作正常,但有一次(参数 ?Format=XML)脚本返回 XML 而不是 HTML。

Is there any way to change the returned mime type of the php output on the fly from text/html to text/xml or application/xml?

有什么方法可以将返回的 php 输出的 mime 类型从 text/html 动态更改为 text/xml 或 application/xml?

回答by nickf

header('Content-type: application/xml');

More information available at the PHP documentation for header()

PHP 文档中提供的更多信息 header()

回答by John Millikin

Set the Content-Typeheader:

设置Content-Type标题:

header('Content-Type: text/xml');

Though you should probably use "application/xml" instead.

尽管您可能应该改用“application/xml”。

回答by andy.gurin

You should send a Content-Typeheader beforeyou send any output.

您应该发送任何输出之前发送Content-Type标头。

header('Content-Type: text/xml');

回答by PhiLho

I will answer to the update, since the previous answers are good.
I have read that Internet Explorer is well known for ignoring Mime type headers (most of the time?) to rely on content of the file (which can cause problems in some cases).

我会回答更新,因为以前的答案很好。
我已经读到 Internet Explorer 以忽略 Mime 类型标头(大部分时间?)而依赖于文件的内容(在某些情况下可能会导致问题)而闻名。

Mmm, I did a simple test:

嗯,我做了一个简单的测试:

<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root><foo a="b">Tada</foo></root>';
?>

Internet Explorer 6 displays it correctly as XML. Even if I remove the xml declaration.
You should indicate which version is problematic.

Internet Explorer 6 将其正确显示为 XML。即使我删除了 xml 声明。
您应该指出哪个版本有问题。

Actually, as I wrote above, with IE (6 at least), you don't even need a content-type, it recognize XML data and display it as a tree. Is your XML correct?

实际上,正如我上面写的,使用 IE(至少 6),您甚至不需要内容类型,它可以识别 XML 数据并将其显示为树。你的 XML 正确吗?

[Update] Tried with IE7 as well, adding ?format=xml too, still displaying XML correctly. If I send malformed XML, IE displays an error. Tested on WinXP Pro SP2+

[更新] IE7 也试过,添加 ?format=xml ,仍然可以正确显示 XML。如果我发送格式错误的 XML,IE 会显示错误。在 WinXP Pro SP2+ 上测试

回答by Usman Ahmed

header('Content-Type: application/xml; charset=utf-8');

You can add encoding as well in the same line. I added utf-8, which is most common.

您也可以在同一行中添加编码。我添加了 utf-8,这是最常见的。

回答by Thomas

I just used the following:
NOTE: I am using "i" for sql improved extension.

我只使用了以下内容:
注意:我使用“i”来改进 sql 扩展。

Start XML file, echo parent node
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>";
echo "<marker>";

Iterate through the rows, printing XML nodes for each

遍历行,为每个打印 XML 节点

while ($row = @mysqli_fetch_assoc($results)){
  // Add to XML document node
  echo '<marker ';
  echo 'id="' . $ind . '" ';
  echo 'name="' . parseToXML($row['name']) . '" ';
  echo 'address="' . parseToXML($row['address']) . '" ';
  echo 'lat="' . $row['lat'] . '" ';
  echo 'lng="' . $row['lng'] . '" ';
  echo 'type="' . $row['type'] . '" ';
  echo '/>';
}

// End XML file
echo "</marker>";