A while back I had to develop a news widget/ticker as part of my PHP website development project.
First part was to fetch news items regularly via RSS feed, cache the feed and parse it for displaying and for this I used MagpieRSS. Second part was to show the parsed news items as scrolling text or news ticker on a web page.
But first things first, check the demo of what we are talking about Demo MagpieRSS News Scroller.
Here is the PHP code for fetching and parsing the news items using MagpieRSS.
First of all, include the Magpie class:
require_once(MAGPIE_DIR.'/rss_fetch.php'); //where MAGPIE_DIR is the path to magpie dir
Provide the url and set number of items to be displayed (Ideally these should come from Database as I did in the said project managed via admin panel of CMS.):
$url = 'http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/business/rss.xml';
$num_of_items = 10; //set $num_of_items = 'All' to show all items;
Now parse the feed and store all the data in the $rss_news_items variable to be displayed in the page later.
if ( $url ) {
$rss = fetch_rss( $url );
$rss_feed_name = 'Channel: ' . $rss->channel['title'] . ' Updated: '. $rss->channel['lastbuilddate'] ;
if($num_of_items=='All') $num_of_items = sizeof($rss->items);
if(sizeof($rss->items) < $num_of_items) $num_of_items = sizeof($rss->items);
$rss_news_items = '';
for ($i=0; $i < $num_of_items; $i++) {
$href = $rss->items[$i]['link'];
$title = $rss->items[$i]['title'];
$description = $rss->items[$i]['description'];
$date = $rss->items[$i]['pubdate'];
$rss_news_items .= ' '.$title.' - '.$date.' '.$description.' ';
}
}
Now having parsed the RSS feed, we need to make news items scroll on page and for this I used this Scrolling News Ticker by Mioplanet.
echo $rss_feed_name.'
'.$rss_news_items.'
';
Don’t forget to place “webticker_lib.js” from Mioplanet website in the same directory as your php page. You may also want to place following css class in your style sheet.
I selected Mioplanet js scroller due to its usefulness. You can set the ticker directon from left to right or otherwise, ticker speed and its styles easily.
.bgwithtext{ color:#000000; background-color: orange; padding: 5px; }
I have put together a working php script with all the dependent files in this zipped folder Download PHP MagpieRSS News Scroller script. Just make sure you have ‘/cache’ directory at the root of your website and it is writable by php script (with chmod permissions 777).
Hope that helps!
Cheers.
broken links
Have you ever used Simple Pie? Check it out.
Yes, I did use it but only few times. that’s actually better than Magpie in many respects and updated too all the time, I used magpie for its ease of use and due to that fact that it’s been around for so long.