As you might be able to tell from the “ what I’m doing” notification to the right, I’m very excited. I try to teach myself new technical things in my free time, because I don’t have much of an opportunity to work on very technical tasks at work anymore. Tonight, I learned a new trick.
I will fully admit that I am a pretty poor code writer. I’ve been meaning to learn PHP for quite some time, and it always seems overwhelmingly difficult compared to ColdFusion, which is what I’ve been using for dynamic pages for quite some time (since version 4, if I recall correctly).
The main task that I’ve wanted to accomplish with PHP is parsing XML. This is a way to bring content from web pages elsewhere, and use it on your own page. This is how the “Now Spinning,” “What I’m Doing,” and “Recent Wine” items all work (basically). On my blog however, this parsing is all handled by WordPress, the software I use. I just tell it where the feed is and it handles the rest. I wanted to figure out how to do this on my own.
And today I did (kind of). I found this nice utility called, Magpie RSS, to assist me. Magpie is an XML parser for PHP that makes things a little easier. I am told that PHP has its own built-in parser, but I found Magpie gave me enough assistance to make this task as easy as I needed it to be. Magpie is free and open source, and will usually work anywhere, as long as your server is running PHP.
To get MagpieRSS running was pretty quick:
- I downloaded the software.
- Uploaded the files
rss_cache.inc, rss_fetch.inc, rss_parse.inc, and rss_utils.inc to a new directory called “magpierss” in the same folder as the page that I wanted to use to parse XML. I also uploaded the folder extlib to the same directory.
- Then I used
<?php require_once('magpierss/rss_fetch.inc'); to invoke the fetch function.
And that’s basically it. Magpie is ready to parse the XML. Now, I had zero base for PHP, so I also had to learn some proper syntax for creating the code to format the data into usable form. I snagged this code from a page in WordPress, and made some minor alterations to get it to work with Magpie.
<?php require_once('magpierss/rss_fetch.inc');
$url = 'http://corkd.com/feed/cellar/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>”;
}
?>
I’m still not positive what each piece of this code does, but, it works, and right now that is all that matters to me. As you can tell the code is fairly lengthy, especially when compared to the ColdFusion code I would need to do the same thing:
<cfhttp url="hhttp://corkd.com/feed/cellar/bmh" method="get">
<cfset quote = xmlParse(cfhttp.filecontent)>
<p><cfoutput>#quote.rss.channel.item[1].description.XmlText#</cfoutput><br />
~ <cfoutput>#quote.rss.channel.item[1].title.XmlText#</cfoutput></p>
This always seemed so much easier to me, but now that I’ve learned some PHP syntax, it’s not all that different.
It’s always nice to accomplish something like this, especially when I’ve been meaning to figure it out for awhile. I’m going to go ahead and pat myself on the back. And now I’m going to go play a game of FIFA ‘08 on the Wii.
Recent Comments