带有utf-8的xml PHP标头

时间:2022-10-24 23:43:57

I'm using the header() function to turn a file into XML standard. The problem is when I use <?php header("Content-type: text/xml; charset=utf-8"); ?> it just renders the <?xml version="1.0"?>, without the enconding/charset. Am I using it wrongly?

我正在使用header()函数将文件转换为XML标准。问题是我使用<?php header(“Content-type:text / xml; charset = utf-8”); ?>它只是呈现<?xml version =“1.0”?>,没有enconding / charset。我错误地使用它了吗?

3 个解决方案

#1


13  

The header() function just modifies HTTP headers. The code you posted sets a Content-Type header, which is important for telling browsers and other clients what type of file you're serving.

header()函数只修改HTTP头。您发布的代码设置了Content-Type标头,这对于告诉浏览器和其他客户端您正在服务的文件类型非常重要。

The <?xml version="1.0"?> line you're talking about is part of the document itself, and not affected by the HTTP headers.

您正在讨论的<?xml version =“1.0”?>行是文档本身的一部分,不受HTTP标头的影响。

Your tags say you're using DOM to create your XML document. If you change your DomDocument constructor to also pass the charset,

您的标签表示您正在使用DOM来创建XML文档。如果您将DomDocument构造函数更改为也传递charset,

$doc = new DomDocument('1.0', 'UTF-8');

It should output that charset in the XML document.

它应该在XML文档中输出该字符集。

#2


2  

header just sets a HTTP header in the result. PHP doesn't do anything else with the value, so it's up to you to make sure it's being used properly.

header只在结果中设置HTTP标头。 PHP不会对该值执行任何其他操作,因此您需要确保正确使用它。

If you're using an XML library to generate your XML (including the prologue), check the documentation for that library. If you're outputting the XML "by hand" (o, you need to add the necessary attribute to the prologue yourself.

如果您使用XML库生成XML(包括序言),请查看该库的文档。如果你“手动”输出XML(o,你需要自己为序言添加必要的属性。

#3


0  

You can also use

你也可以使用

<?php echo '<?xml version"1.0" encoding="UTF-8"?>'; ?>

#1


13  

The header() function just modifies HTTP headers. The code you posted sets a Content-Type header, which is important for telling browsers and other clients what type of file you're serving.

header()函数只修改HTTP头。您发布的代码设置了Content-Type标头,这对于告诉浏览器和其他客户端您正在服务的文件类型非常重要。

The <?xml version="1.0"?> line you're talking about is part of the document itself, and not affected by the HTTP headers.

您正在讨论的<?xml version =“1.0”?>行是文档本身的一部分,不受HTTP标头的影响。

Your tags say you're using DOM to create your XML document. If you change your DomDocument constructor to also pass the charset,

您的标签表示您正在使用DOM来创建XML文档。如果您将DomDocument构造函数更改为也传递charset,

$doc = new DomDocument('1.0', 'UTF-8');

It should output that charset in the XML document.

它应该在XML文档中输出该字符集。

#2


2  

header just sets a HTTP header in the result. PHP doesn't do anything else with the value, so it's up to you to make sure it's being used properly.

header只在结果中设置HTTP标头。 PHP不会对该值执行任何其他操作,因此您需要确保正确使用它。

If you're using an XML library to generate your XML (including the prologue), check the documentation for that library. If you're outputting the XML "by hand" (o, you need to add the necessary attribute to the prologue yourself.

如果您使用XML库生成XML(包括序言),请查看该库的文档。如果你“手动”输出XML(o,你需要自己为序言添加必要的属性。

#3


0  

You can also use

你也可以使用

<?php echo '<?xml version"1.0" encoding="UTF-8"?>'; ?>