Creating Dynamic XML Sitemaps using PHP

Creating Dynamic XML Sitemaps using PHP

In this post we will show you Creating Dynamic XML Sitemaps using PHP, hear for Creating Dynamic XML Sitemaps using PHP we will give you demo and example for implement.

What is an XML Sitemap

All data you have to think about Sitemaps is accessible at http://www.sitemaps.org.

From this site:

Sitemaps are a simple path for website admins to advise web indexes about pages on their destinations that are accessible for slithering. In its most straightforward shape, a Sitemap is a XML document that rundowns URLs for a site alongside extra metadata about every URL (when it was last refreshed, how regularly it typically changes, and how critical it is, in respect to different URLs in the site) with the goal that web crawlers would more be able to keenly creep the website.

Code for Creating Dynamic XML Sitemaps using PHP

hear is code for Creating Dynamic XML Sitemaps using PHP and how to creat Creating Dynamic XML Sitemaps using PHP

<?php
header('Content-type: application/xml');

require_once '../common/settings.php'; // database settings
require_once PROJECT_PATH . '/lib/php_adodb_v5.18/adodb.inc.php';
require_once PROJECT_PATH . '/lib/small_blog_v0.8.0/smallblog.php'; // custom blogging engine
require_once PROJECT_PATH . '/lib/utils/utils.php'; // utility functions: date_decode, now

// configuration
$url_prefix = 'http://www.pontikis.net/blog/';
$blog_timezone = 'UTC';
$timezone_offset = '+00:00';
$W3C_datetime_format_php = 'Y-m-d\Th:i:s'; // See http://www.w3.org/TR/NOTE-datetime
$null_sitemap = '<urlset><url><loc></loc></url></urlset>';

$blog = new smallblog();  // custom blogging engine
$res = $blog->db_connect($blog_db_settings);
if($res === false) {
echo $null_sitemap;
exit; // Database connection error...
} else {

// get all posts meta-data
$posts = $blog->getPosts(0, 0, '', '', '', now($blog_timezone));
if($posts === false) {
echo $null_sitemap;
exit; // Error retreiving posts...
}

$len = count($posts);
for($i = 0; $i < $len; $i++) {
// entities encode URL according http://www.sitemaps.org/protocol.html#escaping
$posts[$i]['url'] = $url_prefix . htmlspecialchars($posts[$i]['url']);
// convert dates to W3C datetime format http://www.sitemaps.org/protocol.html#xmlTagDefinitions
$posts[$i]['date_updated'] = date_decode($posts[$i]['date_updated'], $blog_timezone, $W3C_datetime_format_php) . $timezone_offset;
}

// retrieve max date
$max_date = $posts[0]['date_updated'];
}

$output = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
echo $output;
?>
<url>
<loc>http://www.pontikis.net/blog/</loc>
<lastmod><?php print $max_date ?></lastmod>
<changefreq>daily</changefreq>
</url>
<url>
<loc>http://www.pontikis.net/blog/archive/</loc>
<lastmod><?php print $max_date ?></lastmod>
<changefreq>daily</changefreq>
</url>
<?php for($i = 0; $i < $len; $i++) { ?>
<url>
<loc><?php print $posts[$i]['url'] ?></loc>
<lastmod><?php print $posts[$i]['date_updated'] ?></lastmod>
</url>
<?php } ?>
</urlset>

Hope this code and post will helped you for implement Creating Dynamic XML Sitemaps using PHP
. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve onlincode. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs onlincode.org

Leave a Comment

Your email address will not be published. Required fields are marked *

60  +    =  70

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US