The aim of this article is to describe the steps necessary to take a Blogger blog, and syndicate the content onto a third party web site, using PHP classes and scripts. The core functionality uses the very straightforward SimpleXML class, shipped with all PHP installations, coupled with some basic knowledge of the Google Blogger API and a small piece of PHP scripting to generate the HTML code.
The reason for doing this is to take advantage of the excellent Blogger interface for editing blog entries, and their market position to help promote the blog in question. In addition, it avoids installing blog software on a web server, thus saving time in setup and administration, as well as allowing the web site owner to keep their MySQL database allowance for something more sophisticated.
Above all - the reader should not have to work harder than necessary: so it is better to take advantage of an existing solution than to host one's own.
The first step is to create the PHP code that accesses a feed from a site. These feeds deliver an XML structured document. WordPress, for example, delivers a feed that contains just the summaries of the entries, and a link back to WordPress.com.
To access these feeds, the SimpleXML class provides a very easy interface to the XML document that is delivered by the server. While there are many functions that can be used by the programmer, this article concentrates on delivering a solution, so only covers what is needed to deliver that solution.
To load an XML document from a URL, the following code is used:
This will return an XML document in $xml_document that can be accessed tag by tag. Assuming that the tag names are known, this allows the programmer to retrieve the value associated with a tag as follows:
If the programmer needs to access a child of that tag, then the following can be used:
Since collections of tags are stored as arrays, where there is more than one, the following must be used:
The above allows for nesting, so the whole XML tree can be traversed by using the correct tag names and collections.
All feeds are returned as XML, and if the programmer knows the Blogger feed ID, they can access their own blog using code such as:
The Blogger feed ID can be seen in any link from the Blogger editing and administration interface, as part of the URL. The XML document that is returned has many items, however, the most useful are as follows:
The above means that the content for the second item is accessed with:
echo $xml_document->entry[1]->content;
With that in mind, it is now time to pull the 2 pieces together and create a Web 2.0 mashup.
The idea behind the mashup is to use PHP to pull the content from the Blogger blog, and display it on the programmer's site. A very simple example of a loop to go through each entry is as follows:
This same trick can be used to pull in content from any other RSS feed, including Yahoo! Pipes.