You are here:  » Caching with exec() disabled


Caching with exec() disabled

Submitted by formmailer on Sat, 2007-10-20 09:33 in

David,

I just tried to setup caching according to your post, but unfortunately this method is not going to work on my sites, because my provider doesn't allow exec() in PHP scripts:
PHP Warning: exec() has been disabled for security reasons in /var/www/vhosts/sitename/httpdocs/feed/test2.php on line 22

Do you have other methods/suggestions to enable caching?

/Jasper

Submitted by support on Sat, 2007-10-20 13:25

Hello Jasper,

There is another way to go about it - using fopen() - which I presume works as you are having no problem using it with the parse function. I'm away from home at the moment but please bear with me and i'll work out the code for using fopen() instead of wget in the exact example from the other thread...

Cheers,
David.

Submitted by formmailer on Sat, 2007-10-20 15:25

Hi David,

I think I managed to work around it with a script I found somewhere. It seems to be working. Could you do a quick scan if it's coded well enough and is optimized enough for use with Magic Parser?

<?php
/*
======================================================================
Get, cache, and output contents of a RSS XML file
Author: George at JavaScriptKit.com/ DynamicDrive.com
Created: Feb 1st, 2006. Updated: Feb 1st, 2006
======================================================================
*/
header('Content-type: text/xml');
// -------------------------------------------------------------------
// Enter list of possible RSS feeds to fetch inside array:
// -------------------------------------------------------------------
$rsslist=array(
"CityZapper" => "http://pf.tradetracker.nl/?fid=166&aid=16631&type=xml&encoding=utf-8",
"BizzWinter" => "http://pf.tradetracker.nl/?fid=161&aid=16631&type=xml&encoding=utf-8"
);
$cachefolder="cache"//path to cache directory. No trailing "/". Set dir permission to read/write!
// -------------------------------------------------------------------
// Determine which RSS file to actually fetch
// Based on the value of the "id" parameter of the URL string mapping to the RSS array's key
// -------------------------------------------------------------------
$rssid=$_GET['id'];
$rssurl=isset($rsslist[$rssid])? $rsslist[$rssid] : die("Error: Can't find requested RSS in list.");
$localfile=$cachefolder"/" urlencode($rssurl); //Name cache file based on RSS URL
// -------------------------------------------------------------------
// Get the minutes to cache the local RSS file based on "cachetime" parameter of URL string
// -------------------------------------------------------------------
$cacheminutes=(int) $_GET["cachetime"]; //typecast "cachetime" parameter as integer (0 or greater)
// -------------------------------------------------------------------
// fetchfeed() gets the contents of an external RSS feed,
// and saves its contents to the "cached" file on the server
// -------------------------------------------------------------------
function fetchfeed(){
global 
$rssurl$localfile;
$contents=file_get_contents($rssurl); //fetch RSS feed
$fp=fopen($localfile"w");
fwrite($fp$contents); //write contents of feed to cache file
fclose($fp);
}
// -------------------------------------------------------------------
// outputrsscontent() outputs the contents of a RSS feed using the cached local RSS file
// It checks if a cached version of the RSS feed is available, and if not, creates one first.
// -------------------------------------------------------------------
function outputrsscontent(){
global 
$rssurl$localfile$cacheminutes;
if (!
file_exists($localfile)){ //if cache file doesn't exist
touch($localfile); //create it
chmod($localfile0666);
fetchfeed(); //then populate cache file with contents of RSS feed
}
else if (((
time()-filemtime($localfile))/60)>$cacheminutes//if age of cache file great than cache minutes setting
fetchfeed();
readfile($localfile); //return the contents of the cache file
}
outputrsscontent();
?>

I made it working with Magic Parser like this:

<?php
  
require("MagicParser.php");
  
$records = array(); // global array of records to pick from later
  
if ( ! $_GET['dest'] ) { $_GET['dest']='a';} //Just to make sure something is displayed when no destination is mentioned
  
function myRecordHandler($record)
  {
    if (!
strpos($record["CATEGORIES/CATEGORY-NAME"], $_GET['dest']))  return; // ignore this record (just return) if the field does not contain the destination value
    
global $records;
    
$records[] = $record;
  }
  
MagicParser_parse("http://www.bijnavakantie.nl/feed/cache.php?id=CityZapper&cachetime=60","myRecordHandler");
  
shuffle($records);
  
// now you pick off as many random records as you require....
  
$count 0;
  foreach (
$records as $record) {
  echo 
'<H4><a href="'.$record["PRODUCTURL"].'" target="_blank">'.$record["NAME"].'</a></H4>';
  print 
$record["PRICE"];
  print 
$record["PRICE-CURRENCY"];
  print 
$record["IMAGEURL"];
  print 
$record["CATEGORIES/CATEGORY-NAME"];
  
$count++;
  if (
$count == 5) break;
  }
?>

Thanks in advance!

/Jasper

Submitted by support on Sat, 2007-10-20 19:20

Hello Jasper,

That looks fine - however there is one big Magic Parser optimisation that you should do.

At the moment, you are not supplying a format string value for MagicParser_parse(). This means that the script is actually fetching and reading the XML twice. The first time in order to auto-detect the format string, and the second time to actually parse the file.

In order to work out the format string for the product feed, the easiest way is just to upload it to the demo tool on this website. If you can browse records with the demo tool, look in the text box at the bottom for the format string currently in use. Then, simply take that format string and make it the 3rd parameter to MagicParser_parse(), for example:

MagicParser_parse("http://www.bijnavakantie.nl/feed/cache.php?id=CityZapper&cachetime=60","myRecordHandler","xml|PRODUCTS/PRODUCT/");

(you might even find it actually is xml|PRODUCTS/PRODUCT/ as this is quite common for product feeds....!)

Cheers,
David.

Submitted by formmailer on Sun, 2007-10-21 06:55

Thanks David!

The online tool is really perfect!
Would it be an idea to include this demo within the Magic Parser package? Personally I think this would be a good idea, since it contains a lot of useful examples.
E.g. I tried to echo the format_string value myself, but no luck! :(

/Jasper