You are here:  » RSS Search Results Page


RSS Search Results Page

Submitted by tbbd2007 on Wed, 2009-02-18 03:14 in

David,

I would like to add a searchbox on each page of my website that takes the keywords a user submits and sends them to a page such as search.php which will display the RSS feed results as a page on my website. The feed URL is {link saved}, where *********** are the keywords.

Please can you write me the form code for each page and the search.php page code to display the feed.

Thanks

Stephen

Submitted by support on Wed, 2009-02-18 08:51

Hi Stephen,

As a self contained script, that is straight forward. The script needs to generate a form (always displayed at the top?), and if the keywords=***** is present in the URL (so the form will submit using the GET method) generate the URL, and the parse the results. Have a go with something like this for starters...

<?php
  
require("MagicParser.php");
  
header("Content-Type: text/html; charset=utf-8");  
  print 
"<form method='GET'>";
  print 
"Keywords: <input type='text' name='keywords' value='".htmlentities($_GET["keywords"])."' /> ";
  print 
"<input type='submit' value='Search' />";
  print 
"</form>";
  function 
myRecordHandler($record)
  {
    print 
"<h3><a href='".$record["LINK"]."'>".$record["TITLE"]."</a></h3>";
    print 
"<p>".$record["DESCRIPTION"]."</p>";
  }
  if (
$_GET["keywords"])
  {
    
$url "http://pf.tradedoubler.com/pf/pf?a=1235317&description=(".urlencode($_GET["keywords"]).")&xslUrl=http://img.tradedoubler.com/images/xsl/rss2.xsl&mimeType=application%2Frss%2Bxml&oe=UTF-8";
    
MagicParser_parse($url,"myRecordHandler","xml|RSS/CHANNEL/ITEM/");
  }
?>

Hope this helps!
Cheers,
David.

Submitted by tbbd2007 on Mon, 2009-02-23 06:01

David,

That didn't work, but then I thought that it might be because the feed is created on the fly. I therefore combined the Yahoo! Product Search (http://www.magicparser.com/node/123) and the Cache Code (http://www.magicparser.com/node/136) to create the following searchbox code which I am posting for the benefit of other users.

<?php
  function cacheFetch($url,$age)
  {
    // directory in which to store cached files
    $cacheDir = "cache/";
    // cache filename constructed from MD5 hash of URL
    $filename = $cacheDir.md5($url);
    // default to fetch the file
    $fetch = true;
    // but if the file exists, don't fetch if it is recent enough
    if (file_exists($filename))
    {
      $fetch = (filemtime($filename) < (time()-$age));
    }
    // fetch the file if required
    if ($fetch)
    {
      // shell to wget to fetch the file
      exec("wget -N -O ".$filename." \"".$url."\"");
      // update timestamp to now
      exec("touch ".$filename);
    }
    // return the cache filename
    return $filename;
  }
?>
<?php
  require("MagicParser.php");
  if (!$_GET["description"]) $_GET["description"] = "";
  print "<form method='GET'>";
  print "I am searching for: <input type='text' name='description' value='".htmlentities($_GET["description"])."' /> ";
  print "<input type='submit' value='Search' />";
  print "</form>";
  function myRecordHandler($item)
  {
    print "THIS IS WHERE THE CODE TO PRINT YOUR FEED ITEMS GOES";
  }
  if ($_GET["description"])
  {
  // fetch (if required)
  $filename = cacheFetch("THIS IS WHERE YOUR URL GOES WITH THE DESCRIPTION ADDED".urlencode($_GET["description"]),86400);
  // parse
  MagicParser_parse($filename,"myRecordHandler");
  }
?>