Creating Automatically Updating Webpages with XML and PHP

XML (eXtensible Markup Language) is a method of publishing data from a web page that allows that data to be automatically updated when the content on the page changes. For instance, blogs publish XML feeds when a new story is added; a podcast is basically an XML feed that points to a new audio file. This is incredibly helpful if you want to pull data from somewhere else, but you don’t want to have to re-write your page when someone else’s content changes.

As I mentioned in my previous post, I’ve been using Cork’d to keep track of my wine drinking and storage. Cork’d has a great feature of keeping your journal and cellar separate, but an even better feature whereby they publish each as XML feeds. I thought that it would be cool to have this data listed here, but I had tried to parse XML with PHP, and I failed miserably the last time (PHP is the language that WordPress, which powers this site, is built on).

I was lucky in trying to build these particular pages, because WordPress has a built-in XML parser. I was able to use this code to pull in the data from Cork’d, and publish here in the wine section:


< ?php require_once (ABSPATH . WPINC . '/rss-functions.php');
$url = 'http://corkd.com/feed/journal/bmh';
$rss = fetch_rss( $url );
if ( $rss ) {
echo "Title: " . $rss->channel['title'] . <”p>”;
echo “<ul>”;
$i = 1;
foreach ($rss->items as $item) {
$href = $item['link'];
$title = $item['title'];
$description = $item['description'];
echo “<li>$title<br />$description</li>”;
if ($i == 5 ) break;
$i = $i + 1;
} echo “</ul>”; } ?>


Unfortunately, I can’t remember where I found this snippet of code now, but a big thanks to where ever I got it, because I’ve been meaning to do this for a long time.

For the other WP users out there, this code lies directly beneath < ?php the_content(); ?> and directly above the php that implements paging, in my case I use this:
<?php
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if ($children) { ?>
<ul>
< ?php echo $children; ?>
</ul>
< ?php } ?>

If you prefer to use different arguments, the WP documentation lists all appropriate arguments for the wp_list_pages template tag.

0 Responses to “Creating Automatically Updating Webpages with XML and PHP”


  1. No Comments

Leave a Reply