0

我正在使用如下所示的代码对站点进行 api 调用:

 $xmlData = file_get_contents("http://isbndb.com/api/books.xml?access_key=XXXXXX&index1=isbn&value1=0596002068");

 echo $xmlData;

但是 xmlData 在浏览器上显示时会自动解析为 HTML。例如<title>,返回的 XML 元素(实际上是书名)被转换为 HTML,本质上成为页面标题,而其他 XML 元素显示为没有标签的纯文本。我希望客户端XMLHttpRequest对象从服务器端获取原始 XML 数据。


为什么会发生这种情况,如何确保不会自动解析 XML?

4

2 回答 2

4

PHP just sees it as text. For instance, do echo "<b>Bold</b>"; and it will "automatically" be in bold. It is the browser that processes the HTML and renders it.

This is what htmlspecialchars is for.

于 2012-08-27T15:44:52.983 回答
1

This got nothing to do with php. you spit out elements which browser interprets as HTML (that's why it sets title). Build your html page right, use <pre> tags around your content, or. when needed, send your content with correct content-type header (like text/plain to display your xml for viewing or text/xml for other purposes) so it will not parse your data as html.

于 2012-08-27T15:43:34.940 回答