Please login or register.

Login with username, password and session length

News:

MS Chat now has a Twitter! Follow us for the latest news in the technology sector.


Author Topic: Class: Feed Reader, version 3 released with Atom support added  (Read 1250 times)

Offline Ian

  • Administrator
  • Loyal Member
  • *****
  • Posts: 6,456
  • Browser: Firefox 3.6
  • Operating system: Windows 7 Home Premium
  • Processor: Intel Pentium 4 with HT
Earlier I had released an RSS Feed Reader (Check here)... But it had some problems ::)
 
This time I have totally recoded it and it also uses SimpleXML for parsing the feed, so it shouldn't have many compatibility issues with different types of RSS feeds. ;D

Here is an example of how to use it:
Code: [Select]
<?php
require_once('Reader.class.php');

$reader = new Reader('http://mschat.net/rss.php?feed=blog''./cache');

echo 
'<pre>';
print_r($reader->return_items());
echo 
'</pre>';
?>


Then you will get an array with the RSS/Atom feed stuff :P

Enjoy :D
 
Update: 9-26-2009
I have updated the Reader class to version 3.0, which adds support for Atom feeds. Here is a list of changes:
- User agent changed to PHP/Class Feed Reader v3
- New parameter added to Reader::Reader() (Constructor) and method Reader::read_feed() called $force_update
- Added new methods Reader::is_atom() and Reader::is_rss()
 
Documentation

Constructor
Reader Object Reader([string $feed_url = null[, string $cache_dir = null[, int $cache_length = 3600[, bool $force_update = false]]]]);
string $feed_url - The feed url you want to have read into an array (Can be RSS or Atom).
string $cache_dir - The path to the directory where you want feeds to be cached.
int $cache_length - How long the feed should be cached before being updated, in seconds, defaults to 1 hour.
bool $force_update - If you want to force the feed to be updated even if its cache hasn't timed out.

public void set_feed(string $feed_url);
string $feed_url - The feed url you want to have read into an array (Can be RSS or Atom).

public bool set_cache(string $cache_dir);
string $cache_dir - The path to the directory where you want feeds to be stored. If it doesn't exist, it will attempt to create it.
returns bool - Returns true if the caching directory exists or false if it doesn't and couldn't create it.

private bool cache_exists([string $cache_dir = null]);
string $cache_dir - The directory to be checked for caching. If none is supplied, it will use the one internally, if any.
returns bool - Returns whether or not the directory exists/created successfully, false if it failed or wasn't a directory.

public int set_cache_length([int $cache_length = 3600]);
int $cache_length - How long you want the cache to be used until it is considered expired. Defaults to 3600 seconds (1 hour).
returns int - Returns the new cache length.

public bool read_feed([bool $force_update = false]);
bool $force_update - If you want to force the feed to be updated whether or not it is expired, set to true.
returns bool - If the feed was successfully fetched/read from file and parsed without error, true is returned, otherwise, false.

private string get_feed(string $url[, int $num_redirects = 0[, bool $force_update = false]]);
string $url - The url of the feed which you want to be fetched (Either from the web or a cached file, if enabled).
int $num_redirects - The number of redirects which have already occurred. This parameter is used internally and should not be set. It is only used when fsockopen is used.
bool $force_update - Whether or not you want to force the feed to be fetched again even if the cache has yet to expire.
returns string - Returns the feed which has either been fetched via the web, or cache.

public array return_items([int $num_items = 0]);
int $num_items - The number of items (RSS) or entries (Atom) to be read from the feed. If set to 0, all will be read and returned.
returns array - Returns an array containing all the items/entries of the feed. The indices of the items/entries totally depend on the feed which was read.

public bool is_cached();
returns bool - Returns true if the feed you want read is cached or not (Changed only after read_feed() is called, either directly or through the constructor).

public bool is_read();
returns bool - Tells you whether or not the there is a feed which is read internally from the feed url you have supplied to read_feed() or the constructor.

public int cache_length();
returns int - Returns how long (in seconds) the cache for the feed can not be updated before being expired.

public bool is_atom();
returns bold - Whether or not the feed you supplied was an Atom feed. Returns null if you have yet to call read_feed() or if you gave no feed url to the constructor.

public bool is_rss();
returns bold - Returns the opposite of is_atom() (Unless null, of course) and has the same prerequisites as is_atom();
« Last Edit: September 26, 2009, 09:06:11 AM by aldo »

Offline Chasingu

  • Full Member
  • ***
  • Posts: 125
Re: Class: Feed Reader v2!
« Reply #1 on: June 25, 2009, 04:34:18 PM »
Sounds cool. I am going to use it...

Could you tell me how to get it working?

Need Free Unlimited Image Hosting? Checkout my new site StyImg to find out more.

Offline Ian

  • Administrator
  • Loyal Member
  • *****
  • Posts: 6,456
  • Browser: Firefox 3.6
  • Operating system: Windows 7 Home Premium
  • Processor: Intel Pentium 4 with HT
Re: Class: Feed Reader v2!
« Reply #2 on: June 25, 2009, 06:13:28 PM »
There is an example in the first message ;) But if you need anything else, be more specific please :D

Offline Chasingu

  • Full Member
  • ***
  • Posts: 125
Re: Class: Feed Reader v2!
« Reply #3 on: June 25, 2009, 10:14:11 PM »
How do I select what Items I want the feed to show.

Need Free Unlimited Image Hosting? Checkout my new site StyImg to find out more.

Offline Ian

  • Administrator
  • Loyal Member
  • *****
  • Posts: 6,456
  • Browser: Firefox 3.6
  • Operating system: Windows 7 Home Premium
  • Processor: Intel Pentium 4 with HT
Re: Class: Feed Reader v2!
« Reply #4 on: June 25, 2009, 10:18:13 PM »
You can't really. You can merely select how many items you want returned...

But I suppose you could search the returned array from Reader::fetch();

Offline Chasingu

  • Full Member
  • ***
  • Posts: 125
Re: Class: Feed Reader v2!
« Reply #5 on: June 25, 2009, 10:23:13 PM »
Sorry I said it wrong. I want to be able to set what sites I want to get the data from.

Need Free Unlimited Image Hosting? Checkout my new site StyImg to find out more.

Offline Ian

  • Administrator
  • Loyal Member
  • *****
  • Posts: 6,456
  • Browser: Firefox 3.6
  • Operating system: Windows 7 Home Premium
  • Processor: Intel Pentium 4 with HT
Re: Class: Feed Reader v2!
« Reply #6 on: June 25, 2009, 10:27:01 PM »
Oh... Well:

<?php
require_once('Reader.class.php');

// Replace URL_OF_FEED with the URL to the RSS feed
$reader = new Reader(URL_OF_FEED);

$items $reader->return_items();

/*
  Then if you want to get more, you can do:
$reader->set_feed(URL_OF_FEED);
$reader->read_feed();
$more_items = $reader->return_items();
*/
?>


Then $items will be an array containing the RSS items and its attributes :)

Offline Chasingu

  • Full Member
  • ***
  • Posts: 125
Re: Class: Feed Reader v2!
« Reply #7 on: June 26, 2009, 09:58:01 AM »
Oh thanks. This will be a great update for my site. When I get on my computer later, Spider Problem, Ill check it out,

Need Free Unlimited Image Hosting? Checkout my new site StyImg to find out more.

Offline Ian

  • Administrator
  • Loyal Member
  • *****
  • Posts: 6,456
  • Browser: Firefox 3.6
  • Operating system: Windows 7 Home Premium
  • Processor: Intel Pentium 4 with HT
Re: Class: Feed Reader v2!
« Reply #8 on: June 26, 2009, 10:00:07 AM »
Cool. You should show us your end product. I always like to see how people use my stuff :)

Offline Chasingu

  • Full Member
  • ***
  • Posts: 125
Re: Class: Feed Reader v2!
« Reply #9 on: June 26, 2009, 12:05:38 PM »
Your welcome. Ill tell people when it is done. The spider problem is that I have the fear of spiders, I can't spell it, so when I see a spider in the house I want it killed. Well, when we looked to find it, we could not find it. I have been straying away from my room ever since. That happened yesterday.

Need Free Unlimited Image Hosting? Checkout my new site StyImg to find out more.

Offline Ian

  • Administrator
  • Loyal Member
  • *****
  • Posts: 6,456
  • Browser: Firefox 3.6
  • Operating system: Windows 7 Home Premium
  • Processor: Intel Pentium 4 with HT
Re: Class: Feed Reader v2!
« Reply #10 on: June 26, 2009, 12:06:58 PM »
Weird... Lol.

Offline Chasingu

  • Full Member
  • ***
  • Posts: 125
Re: Class: Feed Reader v2!
« Reply #11 on: June 26, 2009, 06:49:53 PM »
I got over it.

Need Free Unlimited Image Hosting? Checkout my new site StyImg to find out more.

Offline Chasingu

  • Full Member
  • ***
  • Posts: 125
Re: Class: Feed Reader v2!
« Reply #12 on: June 28, 2009, 07:37:23 PM »
In a test...This is what I got...
http://www.gamechaser.net/news.php

Need Free Unlimited Image Hosting? Checkout my new site StyImg to find out more.

Offline Ian

  • Administrator
  • Loyal Member
  • *****
  • Posts: 6,456
  • Browser: Firefox 3.6
  • Operating system: Windows 7 Home Premium
  • Processor: Intel Pentium 4 with HT
Re: Class: Feed Reader v2!
« Reply #13 on: June 28, 2009, 07:40:29 PM »
Could I get the source..? Or at least that line?

Offline Chasingu

  • Full Member
  • ***
  • Posts: 125
Re: Class: Feed Reader v2!
« Reply #14 on: June 29, 2009, 06:06:56 PM »
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

   <head>
      <meta http-equiv="content-type" content="text/html;charset=utf-8" />
      <meta name="generator" content="Adobe GoLive" />
      <title>news</title>
   </head>

   <body>
<?php
require_once('Reader.class.php');

$reader = new Reader('http://app.feed.informer.com/digest3/JKUGXXA7DL.rss');

echo '<pre>';
print_r($reader->return_items(1));
echo '</pre>';
?>
   </body>

</html>

Need Free Unlimited Image Hosting? Checkout my new site StyImg to find out more.