<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ali Qureshi&#039;s Blog &#187; News Scroller</title>
	<atom:link href="http://www.parorrey.com/blog/tag/news-scroller/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.parorrey.com/blog</link>
	<description>random bits on web development</description>
	<lastBuildDate>Thu, 09 Sep 2010 10:45:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<atom:link rel='hub' href='http://www.parorrey.com/blog/?pushpress=hub'/>
		<item>
		<title>Parsing News Feed using PHP &amp; MagpieRSS and making it scroll</title>
		<link>http://www.parorrey.com/blog/php-development/parsing-news-feed-using-php-magpierss-and-making-it-scroll/</link>
		<comments>http://www.parorrey.com/blog/php-development/parsing-news-feed-using-php-magpierss-and-making-it-scroll/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 13:48:13 +0000</pubDate>
		<dc:creator>Ali</dc:creator>
				<category><![CDATA[PHP Development]]></category>
		<category><![CDATA[MagpieRSS]]></category>
		<category><![CDATA[News Scroller]]></category>
		<category><![CDATA[News Ticker]]></category>
		<category><![CDATA[RSS feeds]]></category>
		<category><![CDATA[RSS Parser]]></category>

		<guid isPermaLink="false">http://www.parorrey.com/blog/?p=46</guid>
		<description><![CDATA[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 [...]


Related posts:<ol><li><a href='http://www.parorrey.com/blog/php-development/setting-different-cache-age-for-different-feeds-using-magpie-rss-php-rss-parser/' rel='bookmark' title='Permanent Link: Setting different cache age for different feeds using Magpie RSS &#8211; PHP RSS Parser'>Setting different cache age for different feeds using Magpie RSS &#8211; PHP RSS Parser</a></li>
<li><a href='http://www.parorrey.com/blog/php-development/how-to-fetch-parse-and-parse-cache-display-rss-atom-and-mediarss-feeds-using-simplepie/' rel='bookmark' title='Permanent Link: How to fetch, parse, cache, and display your external RSS and Atom feeds and mediaRSS on your website using SimplePie'>How to fetch, parse, cache, and display your external RSS and Atom feeds and mediaRSS on your website using SimplePie</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.parorrey.com%2Fblog%2Fphp-development%2Fparsing-news-feed-using-php-magpierss-and-making-it-scroll%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.parorrey.com%2Fblog%2Fphp-development%2Fparsing-news-feed-using-php-magpierss-and-making-it-scroll%2F&amp;source=parorrey&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>A while back I had to develop a news widget/ticker as part of my <a href="http://www.parorrey.com/" target="_blank">PHP website development</a> project.</p>
<p>First part was to fetch news items regularly via RSS feed, cache the feed and parse it for displaying and for this I used <a rel="nofollow" href="http://magpierss.sourceforge.net/" target="_blank">MagpieRSS</a>. Second part was to show the parsed news items as scrolling text or news ticker on a web page.</p>
<p>But first things first, check the demo of what we are talking about <a rel="nofollow" href="http://www.parorrey.com/test/news-ticker/" target="_blank">Demo MagpieRSS News Scroller</a>. </p>
<p>Here is the PHP code for fetching and parsing the news items using MagpieRSS.</p>
<p>First of all, include the Magpie class:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span>MAGPIE_DIR<span style="color: #339933;">.</span><span style="color: #0000ff;">'/rss_fetch.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//where MAGPIE_DIR is the path to magpie dir</span></pre></div></div>

<p>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.):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/business/rss.xml'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$num_of_items</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//set  $num_of_items = 'All' to show all items;</span></pre></div></div>

<p>Now parse the feed and store all the data in the <strong>$rss_news_items</strong> variable to be displayed in the page later.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$url</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000088;">$rss</span> <span style="color: #339933;">=</span> fetch_rss<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$url</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$rss_feed_name</span> <span style="color: #339933;">=</span>  <span style="color: #0000ff;">'Channel: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$rss</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">channel</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' Updated: '</span><span style="color: #339933;">.</span> <span style="color: #000088;">$rss</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">channel</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lastbuilddate'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$num_of_items</span><span style="color: #339933;">==</span><span style="color: #0000ff;">'All'</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$num_of_items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rss</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rss</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">items</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$num_of_items</span><span style="color: #009900;">&#41;</span>  <span style="color: #000088;">$num_of_items</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rss</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">items</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$rss_news_items</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$num_of_items</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$href</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rss</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">items</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rss</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">items</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'title'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$description</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rss</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">items</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'description'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$rss</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">items</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pubdate'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000088;">$rss_news_items</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&amp;nbsp;&amp;nbsp;&lt;span class=&quot;bgwithtext&quot;&gt;&lt;b&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$title</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/b&gt; - '</span><span style="color: #339933;">.</span><span style="color: #000088;">$date</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/span&gt;&lt;span class=&quot;linkTextOnly&quot;&gt;&amp;nbsp;&lt;b&gt;&lt;a href=&quot;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$href</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&quot; target=&quot;_blank&quot; rel=&quot;no-follow&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$description</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/a&gt;&lt;/b&gt;&lt;/span&gt;&amp;nbsp;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now having parsed the RSS feed, we need to make news items scroll on page and for this I used this <a rel="nofollow" href="http://www.mioplanet.com/rsc/newsticker_javascript.htm" target="_blank">Scrolling News Ticker by Mioplanet</a>. </p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$rss_feed_name</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;br/&gt;
&lt;div id=&quot;TICKER&quot; STYLE=&quot;overflow:hidden; width:520px&quot;&gt;'</span><span style="color: #339933;">.</span><span style="color: #000088;">$rss_news_items</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&lt;/div&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;webticker_lib.js&quot; language=&quot;javascript&quot;&gt;&lt;/script&gt;'</span><span style="color: #339933;">;</span></pre></div></div>

<p>Don&#8217;t forget to place &#8220;webticker_lib.js&#8221; from <a rel="nofollow" href="http://www.mioplanet.com/rsc/newsticker_javascript.htm" target="_blank">Mioplanet</a> website in the same directory as your php page. You may also want to place following css class in your style sheet.</p>
<p>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.</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.bgwithtext</span><span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span><span style="color: #cc00cc;">#000000</span><span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> orange<span style="color: #00AA00;">;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>I have put together a working php script with all the dependent files in this zipped folder <a rel="nofollow" href="http://www.parorrey.com/downloads/news-ticker.zip">Download PHP MagpieRSS News Scroller script</a>. Just make sure you have &#8216;/cache&#8217; directory at the root of your website and it is writable by php script (with chmod permissions 777).</p>
<p>Hope that helps!</p>
<p>Cheers.</p>
<div style="padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;;">
										<iframe
											style="height:25px !important; border:none !important; overflow:hidden !important; width:340px !important;" frameborder="0" scrolling="no" allowTransparency="true"
											src="http://www.linksalpha.com/social?link=http%3A%2F%2Fwww.parorrey.com%2Fblog%2Fphp-development%2Fparsing-news-feed-using-php-magpierss-and-making-it-scroll%2F&fc=333333&fs=arial&fblname=like">
										</iframe>
										</div>

<p>Related posts:<ol><li><a href='http://www.parorrey.com/blog/php-development/setting-different-cache-age-for-different-feeds-using-magpie-rss-php-rss-parser/' rel='bookmark' title='Permanent Link: Setting different cache age for different feeds using Magpie RSS &#8211; PHP RSS Parser'>Setting different cache age for different feeds using Magpie RSS &#8211; PHP RSS Parser</a></li>
<li><a href='http://www.parorrey.com/blog/php-development/how-to-fetch-parse-and-parse-cache-display-rss-atom-and-mediarss-feeds-using-simplepie/' rel='bookmark' title='Permanent Link: How to fetch, parse, cache, and display your external RSS and Atom feeds and mediaRSS on your website using SimplePie'>How to fetch, parse, cache, and display your external RSS and Atom feeds and mediaRSS on your website using SimplePie</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.parorrey.com/blog/php-development/parsing-news-feed-using-php-magpierss-and-making-it-scroll/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
