CaRP: Caching RSS Parser - Documentation
Getting Started: Free Download | Purchase | Install
Reference: Functions | Plugins | Themes | Full Index
Etc.: Display Formatting | Example Code | Affiliates
Background Refreshing Example
When displaying newsfeeds from slow servers or displaying many newsfeeds on the same page, your own page will load slowly whenever the cache is refreshed. One way to speed up the loading of multiple newsfeeds on a page is to set different cache timing for each using the function CarpConf('cacheinterval',60); (where 60 is the cache interval in minutes). If visitors come to your page often enough, usually only one cache will be refreshed at a time, providing acceptable performance. However, if your page is only rarely accessed, using different cache intervals may not solve the problem.Another solution is to set up background refreshing of your cache files. This is done by setting up a "cron job" to periodically load a web page containing the newsfeeds. When setting up such a system, note the following points:
- The page loaded by the cron job should not be the same page that is loaded by your website visitors. The reason for this is that the cache interval for the page loaded by the cron job needs to be shorter than the interval for the page loaded by your visitors to ensure that the cron job always handles the refresh.
- Another reason for having a different page is that the page loaded by the cron job doesn't need to contain anything but the newsfeed, but the page being shown to visitors will usually contain various other content. See "update_news.php" below for an example.
- It may be best to have a separate web page (a separate file like "update_news.php") and cron job -- ie. a separate "trigger_update.php" or "trigger_update.pl", or a way to pass the address of "update_news.php" in to "trigger_update.*" -- for each newsfeed to avoid problems with timing out on slow or temporarily unavailable newsfeeds.
update_news.php
This file is loaded by "trigger_update.php" or "trigger_update.pl", show below,
to cache a copy of the newsfeed on your server for quick access:
require_once "/path/to/carp.php";
/* set the cache interval to 1 minute (you'll control how often the cache is actually refreshed with your cron settings, not here) */
CarpConf('cacheinterval',1);
CarpCache('http://www.some.where.com/path/to/newsfeed.rdf',
'cache.file.name');
?>
your_webpage.php
Now that you have the newsfeed cached on your server, you can access it on the page where you want it displayed like this:
require_once "/path/to/carp.php";
/* use the CarpConf() function to set up any desired formatting - see the manual and other examples for details */
CarpShow(CarpCachePath().'cache.file.name');
?>
The Cron Job
To trigger update_news.php, you'll need to set up a cron job to execute one of the following two files. The PHP file is simpler, but not all servers will allow you to execute PHP files from cron jobs, and not all PHP installations will allow you to trigger a script this way. If you can't make it work, use trigger_update.pl instead.
Whichever file you use, if your webserver is a UNIX based system (like Linux or xBSD), you'll need to make the file executable. Some FTP programs can do this, or you might log in using SSH or telnet, switch to the folder containing the file, and execute the command "chmod 755 trigger_update.php" (or ".pl").
Refer to the documentation for cron for instructions on how to set up the cron job on your server.
In both cases, the cron job can run on a different webserver from the one that hosts update_news.php. If using trigger_update.pl and running it on a different server, enter the hostname of the computer where the cron job is running in the "$hostname=" line.
trigger_update.php
If necessary, adjust the path on the first line to point to the correct location of "php" on your server. Also change the URL on the fopen line to the URL of update_news.php on your server.
<?php
if ($f=fopen('http://www.yourwebsite.com/path/to/update_news.php','r')) {
while (!feof($f)) fgets($f);
fclose($f);
}
return;
?>
trigger_update.pl
Change "www.yourwebsite.com" (near the top) and "/path/to/update_news.php" (near the bottom) to the appropriate values for your setup. IMPORTANT: The path to update_news.php should NOT be the full path on the server, but the path that a web browser would specify. For example, if a web browser would load the page as http://www.geckotribe.com/rss/update_news.php", the appropriate path would be /rss/update_news.php.
use Socket;
$hostname='www.yourwebsite.com';
$remote_host='www.yourwebsite.com';
$sockaddr = 'S n a4 x8';
$proto=getprotobyname('tcp');
($name, $aliases, $type, $len, $thisaddr) = gethostbyname($hostname);
($name, $aliases, $type, $len, $thataddr) = gethostbyname($remote_host);
$this = pack($sockaddr, &AF_INET, 0, $thisaddr);
$that = pack($sockaddr, &AF_INET, 80, $thataddr);
socket(S, &AF_INET, &SOCK_STREAM, $proto)||die "socket: $!";
bind(S,$this)||die "bind: $!";
connect(S,$that)||die "connect: $!";
select(S); $|=1; select(STDOUT);
print S "GET /path/to/update_news.php HTTP/1.0\nHost: $remote_host\n\n";
while ($line=<S>) { }
close(S);
exit 0;