简体   繁体   English

带有utf-8的xml PHP标头

[英]PHP header for xml with utf-8

I'm using the header() function to turn a file into XML standard. 我正在使用header()函数将文件转换为XML标准。 The problem is when I use <?php header("Content-type: text/xml; charset=utf-8") ; 问题是我使用<?php header("Content-type: text/xml; charset=utf-8") ; ?> it just renders the <?xml version="1.0"?> , without the enconding/charset. ?>它只是呈现<?xml version="1.0"?> ,没有enconding / charset。 Am I using it wrongly? 我错误地使用它了吗?

The header() function just modifies HTTP headers. header()函数只修改HTTP头。 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. 您发布的代码设置了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. 您的标签表示您正在使用DOM来创建XML文档。 If you change your DomDocument constructor to also pass the charset, 如果您将DomDocument构造函数更改为也传递charset,

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

It should output that charset in the XML document. 它应该在XML文档中输出该字符集。

header just sets a HTTP header in the result. header只在结果中设置HTTP标头。 PHP doesn't do anything else with the value, so it's up to you to make sure it's being used properly. PHP不会对该值执行任何其他操作,因此您可以确保正确使用它。

If you're using an XML library to generate your XML (including the prologue), check the documentation for that library. 如果您使用XML库生成XML(包括序言),请查看该库的文档。 If you're outputting the XML "by hand" (o, you need to add the necessary attribute to the prologue yourself. 如果你“手动”输出XML(o,你需要自己为序言添加必要的属性。

你也可以使用

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

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

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