Connected successfully
dailynewspick.com : Tutorial

Tutorial

 

 

Here is a simple tutorial so that you too can learn how to create an RSS reader.

A note from the author... That is how I learn how to do stuff like this. I had a need for an RSS reader. I looked around, got some ideas, and learned how to create something that is pretty dynamic an useful. So I will document how that is done here. I'm still in the middle of launching this thing. So give it a few days.


The people behind php as a language have made this super simple. There is a single function to call that parses the feed.
It basically turns the feed into an array. Then all you have to do is loop through the array to display the results.

1
$rss = simplexml_load_file($url); // XML parser

With that, $rss is now an array containing the feed.

RSS feeds can contain any number of different tags. For the purposes of this exercise, will only be looking for the ones containing:
<Link>
<Title>
<Description>

The chunk of code below is all you need. Here is what it does:

  1. Initializes a counter used to limit the number of items to show
  2. Sets a default $url
  3. Checks the query string for a user suplied feed, and replaces the default if it finds one.
  4. Sends the feed through the parser
  5. Loops through the new array and generates the necessary HTML to make it look nice
  6. Nothing else

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
	$i = 0; // counter
	$url = "http://rss.cnn.com/rss/cnn_topstories.rss"; // url to parse
	$feedIs = isset($_GET['feedIs']) ? $_GET['feedIs'] : "";
	if ($feedIs!='') $url=$feedIs;
	$rss = simplexml_load_file($url); // XML parser

	// RSS items loop
	print '<p id="curFeed">Current feed - '.$rss->channel->title.'</p>';
	foreach($rss->channel->item as $item) {
		if ($i < 10) { // parse only 10 items
				print '<div class="newsItemCont">';
				print '<a href="'.$item->link.'" target="_blank">'.$item->title.'</a><br />';
				$thisItem = $item->description;
				print ''.$thisItem.'<br />';

				print '</div>';
		}
		$i++;
	}
	 ?>

You can literally copy just this code into a blank PHP file and run it without modifying it at all.

It looks like this.

 

Share on Facebook Tweet about this on Twitter Join our Discord Share on LinkedIn Share on Reddit pinterest Email this to someone

 

affordableSharedHost