Thursday, August 14, 2008

Adding RSS Feeds To Build A Niche Store Content Pages


On some Build A Niche Store sites you might want to have a news page to show headlines or stories related to your niche. I see several people struggling with this and want to show you how this is done.

Copy the below code into notepad and save as testnews.php on your harddrive.

<?PHP

//$headline_style = 'news';
$headline_style = '';
$description_style = '';
$feed_url = '';
$show_detail = "";
$insideitem = "";
$tag = "";
$title = "";
$description = "";
$link = "";
$image = "";
$insideimage = "";
$max = 10;
$count = 0;

function render_news($feed_url, $showdetail, $headlinestyle, $detailstyle) {
global $show_detail, $headline_style, $detail_style, $max, $count, $insideitem,

$insideimage;
$insideitem=false;
$insideimage=false;
$count = 0;
$show_detail = $showdetail;
$headline_style = $headlinestyle;
$detail_style = $detailstyle;

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = @fopen($feed_url,"r");
// or die("Error reading RSS data.");
if ($fp) {
while ($data = fread($fp, 4096))
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
fclose($fp);
} else {
echo '<span class="top10articles">Syndicated content not available</span>';
}
echo '';

// Free up memory used by the XML parser
xml_parser_free($xml_parser);
}


function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $description, $link, $image, $insideimage;
if ($insideitem || $insideimage) {
$tag = $name;
}
if ($name == "ITEM" ) {
$insideitem = true;
}
if ($name == "IMAGE") {
$insideimage = true;

}
}

function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link, $image, $insideimage,

$show_detail, $headline_style, $detail_style, $count, $max;

if ($name == "URL") {

$insideimage=false;
$image="";
} else if ($name == "ITEM" && $count < $max) {
$count++;
printf('<a href="%s" class="top10articles"

target="_blank">%s</a><br>',trim($link),trim($title));
if ($show_detail)
printf('<span

class="top10articles">%s</span><br>',trim($description));
else {
echo "";
}
$title = "";
$description = "";
$link = "";
$insideitem = false;
} else if ($count >= $max) {
$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}

function characterData($parser, $data) {
global $insideitem, $tag, $title, $description, $link, $image, $insideimage;
if ($insideimage) {
switch ($tag) {
case "URL":
$image .= $data;
break;
}
}
if ($insideitem ) {
switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "DESCRIPTION":
$description .= $data;
break;
case "LINK":
$link .= $data;
break;
}
}
}

?>

<style type="text/css">
<!--

/****************************************************************/

/*Main Template Links*/
A { color:#000000; text-decoration: none; }
A:hover { color:#FF0000; text-decoration: none;
}
/****************************************************************/
-->
</style>
<font size="3"><b>CNN</b></font>
<br>
<font size="2"><?
render_news("http://rss.cnn.com/rss/cnn_topstories.rss", false, 'news', 'news', 5);
?></font>
<br>
<b><font size="3">Fox News</font></b>
<br>
<font size="2"><?
render_news("http://www.foxnews.com/xmlfeed/rss/0,4313,0,00.rss", false, 'news', 'news', 5);
?></font>
<br>
<b><font size="3">Yahoo</font></b>
<br>
<font size="2"><?
render_news("http://rss.news.yahoo.com/rss/topstories", false, 'news', 'news', 5);
?></font>
<br>


Then upload the file to the root of your domain. When done load the page in your browser to see if it loads correctly.
http://www.yourdomain.com/testnews.php

If it loads the headlines, you did it correctly.

Now create a new content page in your admin section and call the link name on the right Test News and the Page URL test-news.















Now open up the footer section of the template you are using and add the code below to the very top line in the footer.

<?php
if($mainCat=="test-news"){
include("testnews.php");
}
?>


Click save and the load the new content page in your browser.

http://www.yourdomain.com/test-news

It should not be very hard to figure out how to change the feed urls in the script and you can add more feeds by just adding more lines at the bottom, following the pattern. You can also change the fonts and colors as well very easily. You should be able to tailor this to pull feeds on any content page of your site.

Related Posts by Categories



11 comments:

Anonymous said...

Thanks for your code.Your RSS brings up CNN/FOX news headlines etc. If I wanted to have a RSS feed specific to my industry say like Motor Racing.. how would I do this.. Thanks Dave

soggy on August 14, 2008 at 4:01 PM said...

You would first need to find a website in your niche that has a news RSS feed. Simply do a search in Google for rss feeds related to your niche and replace the CNN feed with the feed you want.

soggy on August 14, 2008 at 4:02 PM said...

There are great search engines dedicated to rss feeds alone.

http://www.syndic8.com

Anonymous said...

some feeds generate this error:
XML error: not well-formed (invalid token) at line 2164

also - I assume that the number at the end of the line (5) is the number of feeds to parse and display? Why then does it always display the max (10)? I have changed (and removed) that number and it seems to have no effect. Also render_news only accepts 4 parameters, wouldn't it ignore the fifth?

Also - how to headlinestyle and detailstyle work? You seem to be able to set any parameter or call them anything and there's no definitions in the program for them.

soggy on September 18, 2008 at 8:01 PM said...

JT, sometimes it will have problems with feeds that use strange characters. It always goes by the last number, which is 5 and always works for me so I do not know why you are getting 10. I will gladly grab a look at it if you would like to email me what you have.

Anonymous said...

The code is the code copied and pasted directly from this page (that shows 10 results) - I didn't change a thing.

Also - how to headlinestyle and detailstyle work? You seem to be able to set any parameter or call them anything and there's no definitions in the program for them.

soggy on September 23, 2008 at 10:37 AM said...

@JT, I did some research and am only using the max number function. I have used that script for years on some sites.

I have since moved on to CARP on newer sites as it caches feeds. I will do a post on that for displaying headlines soon. Sorry for the confusion.

Anonymous said...

Thanks, Soggy, for having written this and for posting the link in the BANS forum. It took quite a bit of digging there to actually find this but I'm nothing if not persistent. With your base and a little bit of work on my part, I'm getting the newsfeed on my site just the way I want it.

attals on April 24, 2009 at 9:45 PM said...

Soggy- I've followed your instructions and when I test the RSS through my browser, I receive this:
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/attals/public_html/testnews.php on line 2

Parse error: syntax error, unexpected T_STRING in /home/attals/public_html/testnews.php on line 2

any ideas?

soggy on April 24, 2009 at 9:55 PM said...

If you want to email or PM me the site I will have a look. please include a temporary login i can use.

Dave

attals on April 28, 2009 at 8:20 PM said...

soggy-first mistake, saving the file as 'RTF' instead of .php. '\' was being added to file and causing issue. After removing all the '\' and saving as '.php', still getting the CNN XML error: not well-formed (invalid token) at line 7 '. By removing the text for 'CNN' it's working fine in my test and in my content page. Thanks for the script.

Followers

Twitter Updates

    follow me on Twitter
     

    Build A Niche Store Blog. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted by Bloganol dot com Privacy Policy