Pulling rss with php
A few folks have asked how I pull the blog feeds through to this home page, so here is the code I use, as a little gift for being awesome people, I have pulled it through exactly as I use it so that you can see and toy with a working example in all it nasty detail.
<div id="news">
<h3><span>Web design </span>News and other stuff<span> like general design stuff, freelance issues web site portfolio updates and general guff otherwise known as a bit of chit chat</span>:</h3>
<ul>
<?php
set_time_limit(0);
$file = "http://mccoy.co.uk/blog/wp-rss.php";
$rss_channel = array();
$currently_writing = "";
$main = "";
$item_counter = 0;
function startElement($parser, $name, $attrs) {
global $rss_channel, $currently_writing, $main;
switch($name) {
case "RSS":
case "RDF:RDF":
case "ITEMS":
$currently_writing = "";
break;
case "CHANNEL":
$main = "CHANNEL";
break;
case "ITEM":
$main = "ITEMS";
break;
default:
$currently_writing = $name;
break;
}
}
function endElement($parser, $name) {
global $rss_channel, $currently_writing, $item_counter;
$currently_writing = "";
if ($name == "ITEM") {
$item_counter++;
}
}
function characterData($parser, $data) {
global $rss_channel, $currently_writing, $main, $item_counter;
if ($currently_writing != "") {
switch($main) {
case "CHANNEL":
if (isset($rss_channel[$currently_writing])) {
$rss_channel[$currently_writing] .= $data;
} else {
$rss_channel[$currently_writing] = $data;
}
break;
case "ITEMS":
if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
$rss_channel[$main][$item_counter][$currently_writing] .= $data;
} else {
$rss_channel[$main][$item_counter][$currently_writing] = $data;
}
break;
}
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
// output HTML
if (isset($rss_channel["ITEMS"])) {
if (count($rss_channel["ITEMS"]) > 0) {
for($i = 0;$i < 3;$i++) {
// for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) {
if (isset($rss_channel["ITEMS"][$i]["LINK"])) {
print ("n<li class="sectionhomeintro sectionhomelistitem quiet"><h4>" . $rss_channel["ITEMS"][$i]["CATEGORY"] . "<a href="" . $rss_channel["ITEMS"][$i]["LINK"] . "" >" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a></h4><span>");
} else {
print ("n<li class="sectionhomeintro sectionhomelistitem quiet"><h4>" . $rss_channel["ITEMS"][$i]["CATEGORY"] . "<br />" . $rss_channel["ITEMS"][$i]["TITLE"] . "</h4><span>");
}
print ("" . $rss_channel["ITEMS"][$i]["DESCRIPTION"] . "");
print ("</span></li>");
}
} else {
print ("<li class="notme"><span>There are no articles in this feed.</span></li>");
}
}
?>
</ul>
<a href="http://www.mccoy.co.uk/blog/"><img src="/bucket/furniture_pixels/more.gif" alt="Read the rest of the news" /></a>
</div><!-- close news -->
Any refinements or improvements are most welcome just add a comment.
I would also be interested if anyone has a solution as to how to pull in WordPress post image content. in the same manner!
Hi,
I found your website through Alexa.com (off all places!), and then I saw a link to this blog post on your home page and just had to comment because I also wrote a blog post a while ago that talks about how to display recent items from a blog’s feed on a separate web page. Here is a link to my post: http://blog.thelibzter.com/displaying-wordpress-posts-on-a-separate-web-page. My blog is built on WordPress but the rest of my website was coded by hand… it looks like your blog is built on WordPress too, is your whole site? I wonder if that would make a difference on which way (yours or mine) to use.
Also, to answer your question – I haven’t tried pulling in images from a post either, but maybe I will try to figure that out too. Seems like it would be good to know!
Hi Libby,
Yep this site is hand rolled with the blog being wordpress, this script works with any rss xml that I have tried so far, I’ll check out your post.
Hi Rich,
I was in the middle of redesign of my website the last few weeks- if you visited my site, you probably noticed the mess and mayhem over there… it is much better now though
Did you see the method I use to display blog posts on my home page? What did you think of it compared to your method?