PHP SimpleXML Blogger Syndication

Using the SimpleXML Class in PHP to Syndicate Blog Content Invisibly

© Guy Lecky-Thompson

Web 2.0 Mashup How-To using PHP and SimpleXML to syndicate a Blogger Blog using server side web programming and simple HTTP REST requests, through the Google Blogger API.

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 PHP Code

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:

$xml_document = simplexml_load_file('http://...');

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:

echo $xml_document->tag;

If the programmer needs to access a child of that tag, then the following can be used:

echo $xml_document-tag->child;

Since collections of tags are stored as arrays, where there is more than one, the following must be used:

echo $xml_document->tag[0];
echo $xml_document->tag[0]->child;

The above allows for nesting, so the whole XML tree can be traversed by using the correct tag names and collections.

The Blogger XML API

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:

$xml_document = simplexml_load_file('http://www.blogger.com/feeds/blogID/posts/default');

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:

<feed>
<title>title</title>
<link>link to blog</link>
<author>author</author>
<entry>
<title></title>
<content></content>
<link></link>
</entry>
</feed>

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 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:

foreach ($xml_document->entry as $entry) {
echo $entry->title . "<br/>" . $entry->content;
}

This same trick can be used to pull in content from any other RSS feed, including Yahoo! Pipes.


The copyright of the article PHP SimpleXML Blogger Syndication in PHP Programming is owned by Guy Lecky-Thompson. Permission to republish PHP SimpleXML Blogger Syndication must be granted by the author in writing.



Comments
May 3, 2008 1:23 AM
Guest :
This is great I have been trying to use the ZEND framework for hours and it did not work it seems very pooly exampled from the goole API pages. The examples call stuff and then call it again which causes it to crash!!
Page:
1 Comment:

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo