简体   繁体   English

更改 php 中输出的 MIME 类型

[英]change mime type of output in php

I've got a php script.我有一个 php 脚本。 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.大多数情况下,脚本返回 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?

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

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

Set the Content-Type header:设置Content-Type标头:

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

Though you should probably use "application/xml" instead.尽管您可能应该改用“application/xml”。

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

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

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. Internet Explorer 6 将其正确显示为 XML。 Even if I remove the xml declaration.即使我删除了 xml 声明。
You should indicate which version is problematic.您应该指出哪个版本有问题。

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.实际上,正如我上面写的,使用 IE(至少 6),您甚至不需要内容类型,它可以识别 XML 数据并将其显示为树。 Is your XML correct?你的 XML 正确吗?

[Update] Tried with IE7 as well, adding ?format=xml too, still displaying XML correctly. [更新] IE7 也试过,添加 ?format=xml ,仍然可以正确显示 XML。 If I send malformed XML, IE displays an error.如果我发送格式错误的 XML,IE 会显示错误。 Tested on WinXP Pro SP2+在 WinXP Pro SP2+ 上测试

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,这是最常见的。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM