如何在PHP中读取XML文件并生成XML响应?

时间:2015-07-09 12:31:09

标签: php xml

我是PHP的新手。

我已经阅读了一个XML文件,然后将其写为对PHP调用的响应。

<?php
$my_file = 'myfile.txt';

echo htmlspecialchars(file_get_contents($my_file), ENT_QUOTES);
?>

我试过了,但我想响应是xml内容,但编码为html。 如何返回正确的xml响应?

更新:已解决

<?php
$my_file = 'file.txt';
$handle = fopen($my_file, 'r');
$sentinel = fread($handle,filesize($my_file));
header("Content-type: text/xml; charset=utf-8");
$content = file_get_contents($stations);
echo $content;
?>

感谢。

1 个答案:

答案 0 :(得分:1)

问题不应该是如何生成XML响应,而是如何使浏览器理解响应的内容是XML。
为此,您应该使用标题功能:

header("Content-type: text/xml; charset=utf-8");

这表明响应的内容确实是XML而不是HTML。

相关问题