Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get PHP to echo XML tags?

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();
?>

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)?

like image 331
William Orazi Avatar asked Mar 15 '12 13:03

William Orazi


1 Answers

Add the following code at the beginning of your file:

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

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.

like image 126
Rob W Avatar answered Sep 20 '22 06:09

Rob W