How to get PHP for XML echo tags?

I am working on a site that contains about 3000-4000 dynamically generated pages, and I am looking to update the XML sitemap. I tried to use online generators in the past, and they never seem to fix 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 that PHP doesn't write it to a text file, is there a way to get it to display the actual XML tags (I just want to copy and paste it into a Sitemap)?

+4
source share
3 answers

Add the following code at the beginning of the file:

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

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

+14
source

This is the script I'm using. This is echos in proper XML format for Google to read as a sitemap.

 <?php header("Content-type: text/xml"); $xml_output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; $xml_output .= "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">\n"; $xml_output .= "<url>\n"; $xml_output .= " <loc>http://www.mydomain.com/page1</loc>\n"; $xml_output .= "</url>\n"; $xml_output .= "<url>\n"; $xml_output .= " <loc>http://www.mydomain.com/page2</loc>\n"; $xml_output .= "</url>\n"; $xml_output .= "</urlset>"; echo $xml_output; ?> 
+5
source

You need to avoid tags, otherwise your browser will try to display them:

 echo htmlentities('your xml strings'); 
+3
source

Source: https://habr.com/ru/post/1401601/


All Articles