RDF Parsing Troubles

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
icelord
Forum Newbie
Posts: 8
Joined: Sun May 19, 2002 9:19 pm

RDF Parsing Troubles

Post by icelord »

I got this script from some site and I am using it to display on my own computer. The script works and it displays all the news just like on the site.
But the problem is that I told it to only look for new headers after certain time (after 3 hours), but every time i load the page, it refreshes it. (I noticed it because my cache files are always updated whenver i load the page)

I don't like this because I don't want to be a bastard to the sites and keep requesting from them every minute. Everybody in my house uses the news page to get quick links, so I would hate to waste the sites bandwidth.

Can someone look at the code and tell me if there is a solution to it?

Code: Select all

<?php
//      InsertRDF Documentation
//      InsertRDF($url, $cache_file, $description = true, $max = 10, $cache_time = 3600)
//
//      This is file has a function, InsertRDF, to insert a RDF feed into your website
//
//      Just copy the URL of the RDF and paste it as the first agrument and then
//      create an unique file name for the second agrument. The other arguments are
//      optional.
//
//      A third agrument of TRUE can be added if you want to see descriptions of the news
//      stories, something some RDF news sources provide. It trys to show descriptions by default.
//      
//      $max is the maximum number of items to be displayed. It is 10 by default.
//      $cache_time is the amount of time in seconds that has to pass before it downloads a
//      new version instead of using the cache. Its an hour be default. You probably shouldn't 
//      make this any lower, as I've read it could get your IP banned.
//      
//      Examples: 
//      PHP for Freshmeat.net:
//      <?
//      include("rdf.php");
//      InsertRDF("http://freshmeat.net/backend/fm.rdf", "/tmp/freshmeat.net.cache");
//      ?>
//      PHP for BBC World (with news descriptions):
//      <?
//      include("rdf.php");
//      InsertRDF("http://www.newsisfree.com/HPE/xml/feeds/60/60.xml", "/tmp/bbcworld.cache", TRUE);
//      ?>
//      Some of the code (such as the stuff deciding whether or not to download
//      a new version) comes from the freshmeat.php3 located at http://swamp.chl.chalmers.se/backends/
//
//      The closest thing this script has to a homepage would be http://ian.webhop.org/projects.php 

//Two general purpose functions
function FindLength($AString) //from some comments from the docs at http://www.php.net
&#123;
foreach (count_chars($AString) as $my_value) $length=$length+$my_value;
return $length;
&#125;
//I'm used to REBOL text parsing. PHP probably has better ways of doing this.
function ParseIt($begin, $end, $haystack, $offset = 0) 
&#123;
$StartPos = strpos($haystack, $begin, $offset);
$EndPos = strpos($haystack, $end, $offset);
$ParsedLength = $EndPos - $StartPos;
//$returnthis = substr($haystack, $StartPos, $ParsedLength);
$offset=$EndPos + FindLength($end);
return substr($haystack, $StartPos, $ParsedLength);
&#125;
//You change the default values here
function InsertRDF($url, $cache_file, $description = false, $max = 5, $cache_time = 10800) 
&#123;
$cache_time_rnd =       300 - rand(0, 600);
$time           =       split(" ", microtime());

if ( (!(file_exists($cache_file))) || ((filectime($cache_file) + $cache_time - $time&#1111;1]) +
$cache_time_rnd < 0) || (!(filesize($cache_file))) )
&#123;
 //Put file in string
$rdf = join ('', file ($url));
$fcache = fopen($cache_file, "w");
fputs($fcache, "<br>");
 if(substr_count($rdf, "<item") < $max)
  &#123;
   $max = substr_count($rdf, "<item");
  &#125; 
 $ItemArray = Array();
 //get items
 $offset = 0; 
 for($i=1; $i<=$max; $i++)
  &#123;
   $ItemArray&#1111;$i] = ParseIt("<item", "</item>", $rdf, &$offset);  
  &#125;
 $TitleArray = Array(); 
 $LinkArray = Array();
 $DescriptionArray = Array(); 
 $nobug = true;			//Get around a bug in some newsisfree.com RDFs
 if(strpos($rdf, "</link>"))
 &#123;
  $nobug = true;  
 &#125;
 else
 &#123;
  $nobug = false;
 &#125;
 for($i=1; $i<=$max; $i++)
  &#123;
   $TitleArray&#1111;$i] = strip_tags(ParseIt("<title>", "</title>", $ItemArray&#1111;$i]));
   if($nobug)
   &#123;
   $LinkArray&#1111;$i] = strip_tags(ParseIt("<link>", "</link>", $ItemArray&#1111;$i]));
   &#125;
   else
   &#123;
   $LinkArray&#1111;$i] = strip_tags(ParseIt("<link>", "<description>", $ItemArray&#1111;$i]));
   &#125;
   $DescriptionArray&#1111;$i] = strip_tags(ParseIt("<description>", "</description>", $ItemArray&#1111;$i]));
   fputs($fcache, "<div class=news><img src="images/square.gif" align="absmiddle">&nbsp;<a href="$LinkArray&#1111;$i]">$TitleArray&#1111;$i]</a></div>");
   if($description) fputs($fcache, " $DescriptionArray&#1111;$i]\n");
  &#125;
fputs($fcache, "\n");
fclose($fcache);
&#125;
if (file_exists($cache_file)) &#123;
	include($cache_file);
	&#125;
&#125;
?>
Thanks in advance.

IcE.
Post Reply