Adding feeds with CG-FeedRead - a tutorial
By Murray Bourne, 19 Jan 2006
Someone recently asked for a recommended method for pulling feeds from news sources into a PHP-based website.
Update, 27 Feb 2007: There were some problems with the feeds from Bangladesh and the $dLimit variable. I have rewritten the tutorial where necessary.
I use CG-Feedread by David Chait on my Interactive Mathematics site to pull posts from squareCircleZ blog into the homepage (you can see the links under the heading "Mathematics Blog" in the right column of the math site).
Download the latest CG-FeedRead. It is called something like "WP 1.5.1.x Compatible...". It's free, but Chait appreciates donations.
The script works fine but the documentation is rather hard to figure if you are a newby.
CG FeedRead is designed to work with the WordPress blog engine, but here I am going to assume that we are independent of WordPress.
After you have downloaded the zip file, extract the files. You get lots of files, but will only need 4, from the \plugins\cg-plugins\ directory.
We'll assume the PHP page that you want to pull the feeds into is in the root directory of http://www.mysite.com/.
Create a directory /cg in your root directory (so it will be http://www.mysite.com/cg/) . Upload the following 4 files from the zip into that /cg directory:
[Update 14 Nov 2006: Please see David Chait's comment below, about uploading all of the files, and my comment following.]
Inside the cg/ directory, create a directory called /cache_feedread (so it will be http://www.mysite.com/cg/cache_feedread/). This holds a small cache of the feeds (this is much better than calling the feed everytime a user goes to your PHP page.) CHMOD the permissions for the cache directory to 764. (The script needs to be able to write the cached files. If 764 does not work, try giving more write permissions.)
That's it for installing CG FeedRead.
The PHP page to display feeds (single feed only)
1. Start with the shell of an HTML document (you need all this so it displays properly in a browser), something like:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title></title> </head> <body> </body> </html>
2. Now, before the <!DOCTYPE HTML PUBLIC ... stuff at the very top, put the following:
<?php require_once("cg/cg-feedread.php"); ?>
This tells the current page to load the cg-feedread script.
3. In the <body> section, put the following (this tells the cg-feedread script to go get the feed, process it and display it on this page):
<?php $feedUrl = "https://www.intmath.com/blog/feed"; $maxItemsPerFeed = 4; $showDetails = true; $cacheName = "blog"; $filterCat=''; $tLimit = -1; $dLimit = 10; $noHTML = true; $showTime = false; $feedStyle = false; $noTitle = true; $showTimeGMT = true; $titleImages = false; $multiSiteTitle = false; $makeRSS = false; $rssLink=""; $feedOut = getSomeFeed($feedUrl, $maxItemsPerFeed, $showDetails, $cacheName, $filterCat, $tLimit, $dLimit, $noHTML, $showTime, $feedStyle, $noTitle, $showTimeGMT, $titleImages, $multiSiteTitle, $makeRSS, $rssLink); if ($feedOut) echo $feedOut; ?>
Note 1: I found the listing of these variables in the original files was quite confusing until I rewrote them like this. Takes space, but at least you know what is going on.
Note 2: The line, $dLimit = 10; gives you the first 10 characters of the post. (But see the comments below - this needed an extra tweak to work.)
Example Results Page
You can see the results of the previous step at the top of: CG FeedRead Examples. Also on that page, following the squareCircleZ feed are the feeds from the 2 news sites (The New Nation and The ABC, Australia) that we will combine into one feed in the section below.
4. Save your page with a PHP extension (something like feedread.php will do), load it on your server (in the root directory - see above) and then call it in your browser (it will be www.mysite.com/feedread.php. All should work. (Good luck!)
The PHP page to display multiple feeds
This now answers the original request, for a way to pull multiple feeds into one page. The following is similar to what we had above, but allows for multiple feeds.
1. In the <body> section, this time use the following code:
<?php // first, make an array of all the feeds you want mixed $feeds = array ( "http://timesofindia.indiatimes.com/rssfeedsdefault.cms", "http://abc.net.au/news/syndicate/offbeatrss.xml" ); // decide how many total entries you want, //sampled from how many PER FEED $count = array(10, 5); // 10 max output, 5 max sourced from each feed. $showDetails = true; $cacheName = "arrayOfFeeds"; $filterCat=""; $tLimit = -1; $dLimit = 10; $noHTML = true; $showTime = false; $feedStyle = false; $noTitle = true; $showTimeGMT = true; $titleImages = false; $multiSiteTitle = false; $makeRSS = false; $rssLink=""; $feedOut = getSomeFeed($feeds, $count, $showDetails, $cacheName, $filterCat, $tLimit, $dLimit, $noHTML, $showTime, $feedStyle, $noTitle, $showTimeGMT, $titleImages, $multiSiteTitle, $makeRSS, $rssLink); if ($feedOut) echo $feedOut; ?>
Notice the differences between this and the single feed example above. This time you need to supply an array of the feeds you want and the variables for the function are a bit different.
You can see the result of this step at the bottom of the example page under the heading "Mega Array": CG FeedRead Examples.
2. Once again, save your page with a PHP extension (something like feedread.php), load it on your server (in the root directory - see above) and then call it in your browser. All should work. (Good luck!)
- The first time you try the multifeed version you may get all sorts of messages in your browser window - this just indicates that the caching is going on. Refresh a few times and it should be neat.
- If you get stray messages you don't want (like "CGFR: MultiFeed (2) processing...") just comment those lines out of the cg-feedread.php file (they are for error checking and it's okay to kill them). They look like:
dbglog("CGFR: MultiFeed (2) processing...");
- I have styled the example - see the HTML output to see how it can be done.
- You can play with the variables in the function - refer to the feedreadReadme.htm page that comes with the download.
- I have found that not all feeds work. For example, the xml feed from The New Nation site (http://nation.ittefaq.com/artman/publish/rss.xml) did not work, but appears to be properly formed XML. Maybe a setting I need to tweak. Update: I realise now it didn't work because it is Windows formatted, not UTF-8. That's why I changed to the IndiaTimes feed.
Good luck. Hope it works for you.
See the 29 Comments below.