Saturday, May 1, 2010

Using PHP's XMLWriter to create a basic Google SiteMap

The PHP manual is one of the greatest programming references I have ever used, however when I came across the need to use XML Writer class that is packaged with the later versions of PHP I needed a quick guide to using this amazing class and couldn't find one. So in the spirit of the open source movement I decided to write this little tutorial, it's not exhaustive, but it should get you started and it's handy when I work in different places and I need a quick reference to the class.

I had to create an XML feed for Google Maps and I needed to do it using PHP. As the site was dynamic and the URLs were often changing, it would have become a little tedious to create and update an XML feed by hand. Here I will explain the core methods to create your sitemap in PHP and where to post it on Google Webmaster Tools.

To get you started you need to create an instance of the class. Once that is done we will be using these methods. There are others available, but in this case, these are the only ones we need:

//create the instance
$xml = new XMLWriter();
//assign memory for output
$xml->openMemory();
//start the document
$xml->startDocument();
//create our first element. We use this element when we do not want to close the element after the value has been written
$xml->startElement('urlset');
//this adds the correct namespace for the sitemap specification
$xml->writeAttribute('xmlns','http://www.sitemaps.org/schemas/sitemap/0.9');

//your set of urls, could be generated in a number of ways including a database
$urls = array('http://www.domain.com/index.htm', 'http://www.domain.com/page2.htm', 'http://www.domain.com/page3.htm');

//loop through your urls
foreach ($urls as $url) {
$xml->startElement('url');
$xml->writeElement('loc', $url);
$xml->endElement();
}

//this ends the urlset element
$xml->endElement();
//end the document
$xml->endDocument();

//declare the type of page that you will be creating
header('Content-Type: text/xml');

//flush the content into the page
echo $xml->flush();

There are 3 other optional piecse of information you can add to the url element, these are:

lastmod
changefreq
priority

In terms of adding them in to the above code, you could make the array multi dimensional and change your foreach loop to look something like this:

$urls = array(
$urls[] = array('location' => 'http://www.domain.com/index.htm', 'lastmod' => '2009-12-10', 'changefreq' => 'daily', 'priorty' => '0.8'),
$urls[] = array('location' => 'http://www.domain.com/page2.htm', 'lastmod' => '2009-12-11', 'changefreq' => 'daily', 'priorty' => '0.7'),
$urls[] = array('location' => 'http://www.domain.com/page3.htm', 'lastmod' => '2009-12-12', 'changefreq' => 'daily', 'priorty' => '0.5'),
);

//loop through your urls
//we use write element when are opening and closing a tag at the same time
foreach ($urls as $url) {
$xml->startElement('url');
$xml->writeElement('loc', $url['location']);
$xml->writeElement('lastmod', $url['lastmod']);
$xml->writeElement('changefreq',$url['changefreq']);
$xml->writeElement('priority', $url['priority']);
$xml->endElement();
}



0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home