PHP添加"<头/>"到任何XML输出


PHP adds "<head/>" to any xml output

不知道发生了什么,但这里是我的代码:

$template = '
  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <categories>
    <category>
    </category>
  </categories>';
echo $template;

但是这是网页浏览器显示的结果

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<head/><categories>
  <category>
  </category>
</categories>

当我稍微改变代码时

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
categories>
  <head/><category>
  </category>
</categories>

注意,我删除了"<",然后将头部附加到下一个"<"上。这到底是怎么回事?有人知道这是什么吗?

在浏览器中输出内容之前,您是否正确设置了内容类型头?如果不是,浏览器将默认为text/html,并可能会添加标题标签(虽然不确定)。

header("Content-type: text/xml"); 

,如果浏览器没有添加标记,请确保在响应中除了XML之外没有输出任何其他内容,可能您在HTML标记之后回显内容,并且header没有结束标记(值得检查)。

请记住,头必须在任何其他内容之前,否则将得到一个错误。