943,020 Members | Top Members by Rank

Ad:
  • PHP Code Snippet
  • Views: 3289
  • PHP RSS
0

PHP Rss Reader

by on May 31st, 2010
Hello friends, this code/snippet is nothing special, just a simple rss reader class written in PHP.

Many times in my projects I use this script to get an RSS feed from some website and now i decided to share with you.

This script can be useful for your projects or your education.

If you have any suggest or better solution you can share or you can correct me.


Here an example "How to use this script":
html Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.  
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title> PHP Library - RssAgent, RSS Reader </title>
  7.  
  8. <style type="text/css">
  9. body{font-family: Arial, Helvetica, sans-serif; font-size: 14px; width: 980px; margin: 0 auto; padding-top: 30px; background-color: #EEEEEE;}
  10. ._link { font-size: 16px; }
  11. .pDate { color: #888888; }
  12. .giud_link { text-decoration: none; color: #00AA00; font-size: 12px; }
  13. .content { background-color: #FFFFFF; padding: 25px; border: 1px #BBBBBB solid; }
  14. </style>
  15.  
  16. </head>
  17.  
  18. <body>
  19.  
  20. <div class="content">
  21.  
  22. <?php
  23.  
  24. include("RssAgent.php");
  25. $RSS = new RssAgent( "http://www.daniweb.com/external.php?type=RSS2" );
  26.  
  27. for( $i = 0; $i < sizeof($RSS->title); $i++ )
  28. {
  29. echo '<a class="_link" href="' . $RSS-> link[$i] . '" target="_blank">' . $RSS->title[$i] . '</a> - ';
  30. echo '<span class="pDate"> ' . $RSS->pubDate[$i] . '</span> <br /> ';
  31. echo $RSS->description[$i] . "<br /> ";
  32. echo '<i> Category: ' . $RSS->category[$i] . "</i> <br /> ";
  33. echo '<a class="giud_link" href="' . $RSS-> guid[$i] . '" target="_blank">' . $RSS->guid[$i] . "</a> <br /> ";
  34. echo "<br /> ";
  35. }
  36.  
  37. ?>
  38.  
  39. </div>
  40.  
  41. </body>
  42. </html>
Last edited by Krstevski; May 31st, 2010 at 7:10 pm.
PHP Code Snippet (Toggle Plain Text)
  1. <?php
  2.  
  3. /*
  4. *
  5. * Free Library - RssAgent
  6. *
  7. * Name: RssAgent
  8. * Description: RSS Reader (RSS v2.0, http://cyber.law.harvard.edu/rss/rss.html)
  9. * -> Reading an RSS feed from a website
  10. * Author: Damjan Krstevski - SkyDriver
  11. * License: Freeware, you can use, copy, edit, redistribute and etc...
  12. *
  13. */
  14.  
  15.  
  16. /* ****************************** Start Class ****************************** */
  17. class RssAgent {
  18.  
  19. /* *** Declaring variables *** */
  20. var $title = array(),
  21. $link = array(),
  22. $description = array(),
  23. $category = array(),
  24. $guid = array(),
  25. $pubDate = array();
  26. /* *** End of Declaring variables *** */
  27.  
  28. /* *** Construct *** */
  29. function __construct( $urlFeed = NULL ) {
  30. $content = @file_get_contents( $urlFeed ); // Get the content from URL
  31. $x = new SimpleXmlElement( $content ); // Parse XML using SimpleXmlElement Class
  32. foreach( $x->channel->item as $entry ) // Fill the arrays with the rss feed
  33. {
  34. array_push( $this->title, $entry->title ); // Append title
  35. array_push( $this->link, $entry->link ); // Append link
  36. array_push( $this->description, strip_tags( $entry->description ) ); // Strip HTML tags and append description
  37. array_push( $this->category, $entry->category ); // // Append category
  38. array_push( $this->guid, $entry->guid ); // // Append guid
  39. array_push( $this->pubDate, $entry->pubDate ); // Append pubDate
  40. }
  41. }
  42. /* *** End of Construct *** */
  43.  
  44. }
  45. /* ****************************** End Class ****************************** */
  46.  
  47. ?>
Comments on this Code Snippet
Jun 1st, 2010
0

Re: PHP Rss Reader

Simple and clean script. One suggestion though, instead of all the array_push functions, why not loop trough every attribute from $entry? I've seen some RSS feeds that use something other than 'description' for some reason.
Junior Poster in Training
Lapixx is offline Offline
51 posts
since May 2010
Jun 2nd, 2010
0

Re: PHP Rss Reader

The script is written according to standards of RSS 2.0 to allow simple using:

php Syntax (Toggle Plain Text)
  1. $MyClass = new RssAgent("http://rss_feed_link.com");
  2. echo $MyClass->element[$index];

If someone element is not in RSS then they attributes will be filled with empty string.

e.g if the rss contains just title and link, then the others elements (description, category, guid, pubDate) will have same number of element like a 'title' but their value will be empty string.

However, you can edit the script as you want according to your needs and thanks for your suggestion, maybe will be useful for someone.
Last edited by Krstevski; Jun 2nd, 2010 at 12:32 pm.
Junior Poster
Krstevski is offline Offline
110 posts
since May 2009
Aug 11th, 2011
0

Re: PHP Rss Reader

Hello, I'm a beginner in PHP, and I don't understand all this just yet, but I've tried to copy the code exactly and just input my url, and it returns blank. to text what so ever. This is my URL. http://protineretvest.blogspot.com/feeds/posts/default

What should I do?
Light Poster
ovidiu_b13 is offline Offline
27 posts
since Sep 2010
Message:
Previous Thread in PHP Forum Timeline: Simple Regex Help
Next Thread in PHP Forum Timeline: Quick question character validation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC