Categories
PHP

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="
	";

//to add url's:
	$sitemap.=makeUrlTag ("http://yoursite.com/rss/","", "hourly", "0.6");
//end adding urls

	$sitemap.="";

	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$newLine";
    $urlValue = "";
    $urlClose = "$indent$newLine";
    $locOpen = "$indent$indent";
    $locValue = "";
    $locClose = "$newLine";
    $lastmodOpen = "$indent$indent";
    $lastmodValue = "";
    $lastmodClose = "$newLine";
    $changefreqOpen = "$indent$indent";
    $changefreqValue = "";
    $changefreqClose = "$newLine";
    $priorityOpen = "$indent$indent";
    $priorityValue = "";
    $priorityClose = "$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