Alan T. Miller

PHP web developer among other things...

about this picture

Alan Miller's blog for a Brave New World!

Blog PHP Life and Development PHP XML Sitemap generator

PHP XML Sitemap generator

Quickly and dynamically generate your Google Sitemaps with PHP
Created: January 23, 2010
Comments: 12

I needed a way to dynamically generate XML sitemaps and wanted a simple solution for doing just that. As a result, I wrote this set of PHP classes which allows you to dynamically and programmatically create standards based XML sitemaps for inclusion into Google and other search engines.

It should be said that this article does not go into the finer details of the XML sitemap specifications or making XML sitemaps for specific search engines as that is beyond the scope of this article. For more information about Sitemaps, Wikipedia hosts a page that is a good starting point to learn all you ever wanted to know about XML Sitemaps. Other resources include:

This application provides a convenient way to produce something like the following on the fly:

To make use of the code presented in this article you will need to be using PHP 5 or greater as it takes advantage of features not available in PHP 4. With that said, there are three classes that make up this application:

The "xml_sitemap_generator_config" and "xml_sitemap_entry" classes allows us to use type hinting in our application and are what we use to set up our sitemap. The xml_sitemap_generator class will do the brunt of our work for us (creating the XML sitemap markup code and subsequent file).

Source Code

You can download or view the source code by clicking the appropriate link below. You will find all three classes mentioned above in the file named 'class.xml.sitemap.generator.php'

View
Download

Usage

The first thing you need to do is to create an array of xml_sitemap_entry objects. Each object holds the information needed for one enty in your final sitemap file.

Once you have included the class you're ready to start adding these entries to your sitemap as shown below. Again we are going to create an array of entries, each one of those entries is an instance of the xml_sitemap_entry class. For demonstration purposes, we are manually creating the entries array here, however in real life, this array might come from a database or some other method.

Next you need to create the configuration object and define its four attributes: The domain name for the site, the path (i.e. directory) where you plan to write out the sitemap file, and then the filename of the file you want to create (i.e. sitemap.xml) as well as the array of xml_sitemap_entry objects created earlier. Then you simplet instantiate the sitemap generator class by passing in the configuration object as shown below:

At this point all that is left to do is to call the write() method on the class.

Result

As you can see the application does all the dirty work for you and creates the XML sitemap file as shown below. In this case I chose to use the write() method which writes the output to disk, I could have just as easily used the getXml() method and instead of writing the XML to a file, it would have returned it as a string.

Example

When you put it all together it looks something like this:

Final Thoughts

While this provided for my needs at the time I wrote it, there is a lot more than can be done with this application to support rss feeds and sitemap index files as well. You are more than welcome to join me in improving the code to support other formats and functionality.

Comments

There are currently no comments on this article. You could be the first!

Leave Comment