如何让 PHP 回显 XML 标签? [英] How do I get PHP to echo XML tags?

查看:31
本文介绍了如何让 PHP 回显 XML 标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个包含大约 3,000-4,000 个动态生成页面的站点,我希望更新 XML 站点地图.我过去曾尝试使用在线生成器,但它们似乎从未正确捕获所有页面,所以我只是要自己做点什么.基本上我有类似的东西:

I'm working on a site that has about 3,000-4,000 dynamically generated pages, and I'm looking to update the XML sitemap. I've tried using online generators in the past, and they never seem to capture all the pages correctly, so I was just going to do something myself. Basically I have something like:

<?php
require('includes/connect.php');
$stmt = $mysqli->prepare("SELECT * FROM db_table ORDER BY column ASC");
$stmt->execute();
$stmt->bind_result($item1, $item2, $item3);
while($row = $stmt->fetch()) {
    echo '<url><br />
    <loc>http://www.example.com/section/'.$item1.'/'.$item2.'/'.$item3.'</loc>
    <br />
    <lastmod>2012-03-15</lastmod>
    <br />
    <changefreq>monthly</changefreq>
    <br />
    </url>
    <br />
    <br />';
}
$stmt->close();
$mysqli->close();
?>

现在还没有让 PHP 将其写入文本文件,有没有办法强制它回显实际的 XML 标记(我只想将其复制并粘贴到我的站点地图文件中)?

Now short of having PHP write it to a text file, is there a way that I can force it to echo the actual XML tags (I just want to copy and paste it into my sitemap file)?

推荐答案

在文件开头添加以下代码:

Add the following code at the beginning of your file:

header('Content-Type: text/plain');

通过使用此标头提供响应,浏览器不会尝试将其解析为 XML,而是将完整响应显示为纯文本.

By serving the response using this header, the browser will not try to parse it as XML, but show the full response as plain text.

这篇关于如何让 PHP 回显 XML 标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆