PHP sitemap.xml generator

Any web page should use a sitemap.xml, it’s really important for the bots to crawl your site properly.

But how to make it work using PHP?

This is the way I do it for Open Classifieds:

First of all I use to defines, for the path of the file, and when it would expire:

define(SITEMAP_FILE,"sitemap.xml.gz");
define(SITEMAP_EXPIRE,3600); //seconds

At the beginning of any of your scripts we check if the sitempa it’s expired, if it’s expired we generate another time the sitemap :

if (time()>(filemtime(SITEMAP_FILE)+SITEMAP_EXPIRE)){
	generateSitemap();// the sitemap is expired then generates it
}

Then I use this function to generate the file, everytime it’s needed.

function generateSitemap(){//generates the sitemap returns the xml
	$file=SITEMAP_FILE;
	$sitemap="<?xml version='1.0' encoding='UTF-8'?>
	<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'>";
 
//to add url's:
	$sitemap.=makeUrlTag ("http://yoursite.com/rss/","", "hourly", "0.6");
//end adding urls	
 
	$sitemap.="</urlset>";
 
	if (file_exists($file))  unlink ($file);
	$gzdata = gzencode($sitemap, 9);
        $fp = fopen($file, "w");
        fwrite($fp, $gzdata);
        fclose($fp);
 
	return $sitemap;
}

Also we need this functions that I use from smart-it-consulting to make everything work good:

function makeUrlString ($urlString) {
    return htmlentities($urlString, ENT_QUOTES, 'UTF-8');
}
 
function makeIso8601TimeStamp ($dateTime) {
    if (!$dateTime) {
        $dateTime = date('Y-m-d H:i:s');
    }
    if (is_numeric(substr($dateTime, 11, 1))) {
        $isoTS = substr($dateTime, 0, 10) ."T"
                 .substr($dateTime, 11, 8) ."+00:00";
    }
    else {
        $isoTS = substr($dateTime, 0, 10);
    }
    return $isoTS;
}
 
function makeUrlTag ($url, $modifiedDateTime, $changeFrequency, $priority) {
    GLOBAL $newLine;
    GLOBAL $indent;
    GLOBAL $isoLastModifiedSite;
    $urlOpen = "$indent<url>$newLine";
    $urlValue = "";
    $urlClose = "$indent</url>$newLine";
    $locOpen = "$indent$indent<loc>";
    $locValue = "";
    $locClose = "</loc>$newLine";
    $lastmodOpen = "$indent$indent<lastmod>";
    $lastmodValue = "";
    $lastmodClose = "</lastmod>$newLine";
    $changefreqOpen = "$indent$indent<changefreq>";
    $changefreqValue = "";
    $changefreqClose = "</changefreq>$newLine";
    $priorityOpen = "$indent$indent<priority>";
    $priorityValue = "";
    $priorityClose = "</priority>$newLine";
 
    $urlTag = $urlOpen;
    $urlValue     = $locOpen .makeUrlString("$url") .$locClose;
    if ($modifiedDateTime) {
     $urlValue .= $lastmodOpen .makeIso8601TimeStamp($modifiedDateTime) .$lastmodClose;
     if (!$isoLastModifiedSite) { // last modification of web site
         $isoLastModifiedSite = makeIso8601TimeStamp($modifiedDateTime);
     }
    }
    if ($changeFrequency) {
     $urlValue .= $changefreqOpen .$changeFrequency .$changefreqClose;
    }
    if ($priority) {
     $urlValue .= $priorityOpen .$priority .$priorityClose;
    }
    $urlTag .= $urlValue;
    $urlTag .= $urlClose;
    return $urlTag;
}

And that should be all ;)

Now you can submit your sitemap.xml to the google webmaster tools!

UPDATED: Now creates the file with .gz

Related Posts Related Websites
Help sharing and Flatter me ;)

Share

One Comment

  1. [...] Not to long a go we explained how to generate a sitemap.xml with PHP. [...]

Leave a Reply

Follow me