You are here:  » Creating an rss feed

Active Forum Topics


Creating an rss feed

Submitted by wilkins on Tue, 2007-05-29 08:11 in

hi

Is it possible to create an dynamic RSS feed for the products of each merchant, at present the sitemap is gettting our products listed OK on google, however we would like to provide an RSS feed for the products so this could be used if people wanted them to display on their site.

Any help would be great

Thanks

Brent

Submitted by support on Tue, 2007-05-29 13:54

Hi Brent,

RSS is not ideally suited towards product export where there are likely to be thousands of products, however it is quite straight forward to dump a merchant's products as an RSS feed.

rss.php

<?php
  require("includes/common.php");
  header("Content-Disposition: attachment; filename=products.xml");
  header("Content-Type: text/xml");
  print "<?xml version='1.0' encoding='UTF-8'?>";
  print "<rss version='2.0'>";
  print "<channel>";
  print "<title>RSS Product Export</title>";
  print "<link>http://www.example.com/</link>";
  print "<description>RSS Product Export</description>";
  $sql = "SELECT * FROM `".$config_databaseTablePrefix."products` WHERE merchant='".database_safe($_GET["merchant"])."'";
  if (database_querySelect($sql,$rows))
  {
    foreach($rows as $row)
    {
      print "<item>";
      if ($config_useRewrite)
      {
        $href = "product/".tapestry_hyphenate($row["name"]).".html";
      }
      else
      {
       $href = "products.php?q=".urlencode($row["name"]);
      }
      print "<title>".$row["name"]."</title>";
      print "<link>http://www.example.com".$config_baseHREF.$href."</link>";
      print "<description><![CDATA[".$row["description"]."]]></description>";
      print "</item>";
    }
  }
  print "</channel>";
  print "</rss>";
?>

Remember to change http://www.example.com in the above code to your URL so that the links are correct. Then, to use the export, the URL would be:

http://www.example.com/rss.php?merchant=Merchant

Hope this helps get you started...

Cheers,
David.

Submitted by wilkins on Tue, 2007-05-29 14:44

hi

I seem to be getting an error with the above code

Parse error: syntax error, unexpected ';' in /home/utodayno/public_html/rss.php on line 9

Any ideas

Brent

Submitted by support on Tue, 2007-05-29 17:08

Hi Brent,

There was a missing bracket on the call to database_safe() - i've fixed it now in the above code...

Cheers,
David.

Submitted by dflsports on Tue, 2007-12-11 18:36

I've tried without success, but would it be possible to modify the code above to work with a search query?

Maybe the url would look like this, http://example.com/rss.php?q=baseball%20bat and the rss feed created would include all items that matched the search term?

My php skills are weak :) I tried to modify searchresults.php to spit out an rss feed but could not get it to work.

Submitted by support on Tue, 2007-12-11 19:32

Hello Don,

Yes - this is quite straight forward. What's needed is the search code from search.php (that constructs the SQL based on $q from the URL) in place of the single line of SQL contained in the above rss.php script. Here's the basic idea:

Usage: rss.php?q=query

rss.php

<?php
  require("includes/common.php");
  header("Content-Disposition: attachment; filename=products.xml");
  header("Content-Type: text/xml");
  print "<?xml version='1.0' encoding='UTF-8'?>";
  print "<rss>";
  print "<channel>";
  $q = $_GET["q"];
  if (strlen($q) > 3)
  {
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, MATCH name AGAINST ('".database_safe($q)."') AS relevance FROM `".$config_databaseTablePrefix."products` WHERE MATCH name AGAINST ('".database_safe($q)."') GROUP BY name";
  }
  else
  {
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants FROM `".$config_databaseTablePrefix."products` WHERE search_name LIKE '%".database_safe($q)."%' GROUP BY name";
  }
  if (database_querySelect($sql,$rows))
  {
    foreach($rows as $row)
    {
      print "<item>";
      if ($config_useRewrite)
      {
        $href = "product/".tapestry_hyphenate($row["name"]).".html";
      }
      else
      {
        $href = "products.php?q=".urlencode($row["name"]);
      }
      print "<title>".$row["name"]."</title>";
      print "<link>http://www.example.com".$config_baseHREF.$href."</link>";
      print "<description>".$row["description"]."</description>";
      print "</item>";
    }
  }
  print "</channel>";
  print "</rss>";
?>

Hope this helps!
Cheers,
David.

Submitted by dflsports on Wed, 2007-12-12 03:52

Yeah, that worked! Thank you very much!

Submitted by mally on Wed, 2007-12-12 11:24

Hi all

The above gets me to download a xml file, is it possible to show a list on the screen without the download?

Mally

Submitted by support on Wed, 2007-12-12 11:26

Hi Mally,

The download is forced by this line:

  header("Content-Disposition: attachment; filename=products.xml");

If you just delete or comment out that line, a browser should just display the XML instead of offering it for download.

Cheers,
David.

Submitted by wilkins on Mon, 2008-01-07 22:33

Hi David

I have been using this feed for some time, my question is there any way to get images of the product in this feed.

thanks

brent

Submitted by support on Mon, 2008-01-07 22:38

Hi Brent,

Yes- that's straight forward; the image URL is in the result set as $row["image_url"], although it's not used. If you wanted to add it as a simple image tag; where you currently have:

      print "<description>".$row["description"]."</description>";

...add the following line:

      print "<image>".$row["image_url"]."</image>";

However, that isn't standard RSS. If you want to be strictly RSS, an image needs to be specified through an enclosue tag, so you would do it like this:

      print "<enclosure src='".$row["image_url"]."' type='image' />";

(although that version is more complex to parse, so if you don't need to do this i'd stick with the first version)

Cheers,
David.

Submitted by coyote on Tue, 2008-01-29 14:42

Thank you for this code David

It works fine for me

I wonder how i could make appear only the 10 or 20 last items in the rss ?

Moreover, is it possible to have a similar rss for categories ?

Cheers,
Coyote

Submitted by support on Tue, 2008-01-29 15:15

Hi,

To get the last products, you should be able to sort by ID descending; so where you have the following block of code in the above script (the version which accepts a query):

  if (strlen($q) > 3)
  {
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, MATCH name AGAINST ('".database_safe($q)."') AS relevance FROM `".$config_databaseTablePrefix."products` WHERE MATCH name AGAINST ('".database_safe($q)."') GROUP BY name";
  }
  else
  {
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants FROM `".$config_databaseTablePrefix."products` WHERE search_name LIKE '%".database_safe($q)."%' GROUP BY name";
  }

...change this to:

  if (strlen($q) > 3)
  {
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, MATCH name AGAINST ('".database_safe($q)."') AS relevance FROM `".$config_databaseTablePrefix."products` WHERE MATCH name AGAINST ('".database_safe($q)."') GROUP BY name ORDER BY id DESC limit 20";
  }
  else
  {
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants FROM `".$config_databaseTablePrefix."products` WHERE search_name LIKE '%".database_safe($q)."%' GROUP BY name ORDER BY id DESC limit 20";
  }

If you're not using this modified version, simply add:

ORDER BY id DESC limit 20

...to the end of the line that constructs the SQL and that should do the trick.

Here's a quick go at a categories feed:

category_rss.php

<?php
  require("includes/common.php");
  header("Content-Disposition: attachment; filename=categories.xml");
  header("Content-Type: text/xml");
  print "<?xml version='1.0' encoding='UTF-8'?>";
  print "<rss>";
  print "<channel>";
  $sql = "SELECT DISTINCT(category) as category FROM `".$config_databaseTablePrefix."products` ORDER BY category";
  if (database_querySelect($sql,$rows))
  {
    foreach($rows as $row)
    {
      print "<item>";
      if ($config_useRewrite)
      {
        $href = "category/".tapestry_hyphenate($row["category"])."/";
      }
      else
      {
       $href = "search.php?q=category:".urlencode($row["category"]);
      }
      print "<title>".$row["category"]."</title>";
      print "<link>http://www.example.com".$config_baseHREF.$href."</link>";
      print "<description>".$row["category"]."</description>";
      print "</item>";
    }
  }
  print "</channel>";
  print "</rss>";
?>

Remember to change www.example.com to your URL!

Cheers,
David.

Submitted by agape on Thu, 2008-06-12 03:59

Hi,

How do I get the above rss feed to only display 20 items?

Do I need to use a cron job to update the feeds?

Lastly, is it possible to display a feeds that show a variety of different merchants?

Thank You

Submitted by support on Thu, 2008-06-12 04:35

Hi,

Both of these can be achieved simply by changing the SQL, currently:

  $sql = "SELECT * FROM `".$config_databaseTablePrefix."products` WHERE merchant='".database_safe($_GET["merchant"])."'";

To limit to 20 items, add "LIMIT 20" to the end, for example:

  $sql = "SELECT * FROM `".$config_databaseTablePrefix."products` WHERE merchant='".database_safe($_GET["merchant"])."' LIMIT 20";

To include products from all merchants, the WHERE clause can simply be removed:

  $sql = "SELECT * FROM `".$config_databaseTablePrefix."products` LIMIT 20";

(originally, the script takes the merchant from the URL in the ?merchant= parameter)

Or, to include products from selected merchants:

  $sql = "SELECT * FROM `".$config_databaseTablePrefix."products` WHERE merchant IN ('Merchant 1','Merchant 2','Merchant 3') LIMIT 20";

Hope this helps!

Cheers,
David.

Submitted by agape on Thu, 2008-06-12 05:57

Hi David,

Thank you for the quick response. Can you please provide the same example for displaying options when using the rss.php example originally posted above by Don?

Thanks

Pat

Submitted by support on Thu, 2008-06-12 08:49

Hi Pat,

I assume you're referring to the version that returns results based on a query (q=xxx in the URL) rather than per merchant...

  if (strlen($q) > 3)
  {
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, MATCH name AGAINST ('".database_safe($q)."') AS relevance FROM `".$config_databaseTablePrefix."products` WHERE MATCH name AGAINST ('".database_safe($q)."') GROUP BY name";
  }
  else
  {
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants FROM `".$config_databaseTablePrefix."products` WHERE search_name LIKE '%".database_safe($q)."%' GROUP BY name";
  }

In this case, the modification to limit to 20 results would be:

  if (strlen($q) > 3)
  {
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, MATCH name AGAINST ('".database_safe($q)."') AS relevance FROM `".$config_databaseTablePrefix."products` WHERE MATCH name AGAINST ('".database_safe($q)."') GROUP BY name LIMIT 20";
  }
  else
  {
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants FROM `".$config_databaseTablePrefix."products` WHERE search_name LIKE '%".database_safe($q)."%' GROUP BY name LIMIT 20";
  }

And similarly, to restrict this to certain merchants, the same IN statement can be added to the WHERE clauses in each case:

  if (strlen($q) > 3)
  {
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, MATCH name AGAINST ('".database_safe($q)."') AS relevance FROM `".$config_databaseTablePrefix."products` WHERE MATCH name AGAINST ('".database_safe($q)."') AND merchant IN ('Merchant 1','Merchant 2','Merchant 3') GROUP BY name LIMIT 20";
  }
  else
  {
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants FROM `".$config_databaseTablePrefix."products` WHERE search_name LIKE '%".database_safe($q)."%' AND merchant IN ('Merchant 1','Merchant 2','Merchant 3') GROUP BY name LIMIT 20";
  }

For more or less merchants (or just a single merchant) simply add or remove merchants from the list in brackets after the merchant IN...

Cheers,
David.

Submitted by mally on Sat, 2009-01-17 16:15

Hello David

Would it be possible to provide the code to select a rss feed for a particular category, so instead of searching for a product, your searching for items in a particular product? Thanks Mally

Submitted by mally on Sat, 2009-01-17 17:43

Hello David

Ref the above, you mentioned to add print "<image>".$row["image_url"]."</image>"; to get the images

How would you get the cheapest price?

Thanks MAlly

Submitted by support on Mon, 2009-01-19 08:50

Hi Mally,

It's in the minPrice variable in $row, so something like:

print "<lowestPrice>".$row["minPrice"]."</lowestPrice>";

Cheers,
David.

Submitted by mally on Mon, 2009-01-19 17:17

Hello David

Would it be possible to provide the code to select a rss feed for a particular category, so instead of searching for a product, your searching for items in a particular product? Thanks Mally

Great stuff

Submitted by support on Mon, 2009-01-19 17:30

Hi Mally,

Not quite sure what you mean there - are you looking to create an index page with every category name being a link to it's RSS feed URL?

Cheers,
David.

Submitted by mally on Mon, 2009-01-19 22:03

Hi David,

Not sure if its possible but instead of using a rss then keyword, I would like to do rss and a category name (e.g
Fishing Magazines
), so basically the rss feed display's all the items and their details. so in this case it would be about 12 fishing mags

Hope this makes sense

Cheers Mally

Submitted by support on Tue, 2009-01-20 10:18

Hi Mally,

That's straight forward - if you want to email me your existing rss.php i'll modify it to support this, then you can add a feed link to the category index pages...

Cheers,
David.

Submitted by dflsports on Fri, 2009-01-23 16:09

I still have the rss.php code working but I'd really love to insert the image and price in the description of the rss feed. Has anyone accomplished this using cdata?

I can't get the image url to show up at all,

code I'm using

I used
print "<enclosure src='".$row["image_url"]."' type='image' />";
to experiment with the image url but it always shows up black

<?php
  require("includes/common.php");
  header("Content-Type: text/xml");
  print "<?xml version='1.0' encoding='UTF-8'?>";
  print "<rss>";
  print "<channel>";
  $q = $_GET["q"];
  if (strlen($q) > 3)
  {
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, MATCH name AGAINST ('".database_safe($q)."') AS relevance FROM `".$config_databaseTablePrefix."products` WHERE MATCH name AGAINST ('".database_safe($q)."') AND merchant IN ('HockeyGiant.com') GROUP BY name ORDER BY id DESC limit 20";
  }
  else
  {
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants FROM `".$config_databaseTablePrefix."products` WHERE search_name LIKE '%".database_safe($q)."%' AND merchant IN ('HockeyGiant.com') GROUP BY name ORDER BY id DESC limit 20";
  }
  if (database_querySelect($sql,$rows))
  {
    foreach($rows as $row)
    {
      print "<item>";
      if ($config_useRewrite)
      {
        $href = "product/".tapestry_hyphenate($row["name"]).".html";
      }
      else
      {
        $href = "products.php?q=".urlencode($row["name"]);
      }
      print "<title>".$row["name"]."</title>";
      print "<link>http://www.hockeyshopper.com".$config_baseHREF.$href."</link>";
      print "<description>".$row["description"]."</description>";
print "<enclosure src='".$row["image_url"]."' type='image' />";
      print "</item>";
    }
  }
  print "</channel>";
  print "</rss>";
?>

Submitted by Nigel A on Wed, 2009-04-22 21:59

Hi Dave, when i use this bit of programming i get a feed code error.

Thanks

Nigel

Submitted by support on Thu, 2009-04-23 07:44

Hi Nigel,

What is the error message you are getting exactly? Can email me your script and screen shot or copy and paste the error text?

Cheers,
David.

Submitted by Deanh01 on Tue, 2009-05-26 22:34

Hi David
I am also getting an error

Only one top level element is allowed in an XML document.
Line: 2 Character: 2

Parse error: syntax error, unexpected T_ELSE in /home/example/public_html/rss3.php on line 17

Here is the code

<?php
  require("includes/common.php");
  header("Content-Type: text/xml");
  print "<?xml version='1.0' encoding='UTF-8'?>";
  print "<rss>";
  print "<channel>";
  $q = $_GET["q"];
  if (strlen($q) > 3)
  {
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants, MATCH name AGAINST ('".database_safe($q)."') AS relevance FROM `".$config_databaseTablePrefix."products` WHERE MATCH name AGAINST ('".database_safe($q)."') AND merchant IN ('Flirty Lingerie','Eden Fantasys','Wicked Temptations') GROUP BY name LIMIT 20";
  }
  else
  {
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants FROM `".$config_databaseTablePrefix."products` WHERE search_name LIKE '%".database_safe($q)."%' AND merchant IN ('Flirty Lingerie
','Eden Fantasys','Wicked Temptations') GROUP BY name LIMIT 20";
  }
  else
  {
    $sql = "SELECT * , MIN( price ) AS minPrice, MAX( price ) AS maxPrice, COUNT( id ) AS numMerchants FROM `".$config_databaseTablePrefix."products` WHERE search_name LIKE '%".database_safe($q)."%' GROUP BY name";
  }
  if (database_querySelect($sql,$rows))
  {
    foreach($rows as $row)
    {
      print "<item>";
      if ($config_useRewrite)
      {
        $href = "product/".tapestry_hyphenate($row["name"]).".html";
      }
      else
      {
        $href = "products.php?q=".urlencode($row["name"]);
      }
      print "<title>".$row["name"]."</title>";
      print "<link>http://".$_SERVER["HTTP_HOST"].$config_baseHREF.$href."</link>";
      print "<description>".$row["description"]."</description>";
      print "<enclosure src='".$row["image_url"]."' type='image' />";
      print "</item>";
    }
  }
  print "</channel>";
  print "</rss>";
?>

Any help will be appreciated

Thank You
Dean

Submitted by support on Wed, 2009-05-27 08:09

Hi Dean,

The code looks fine as far as I can tell! Could you perhaps email me a link to the feed so that I can view the actual code generated...

Cheers,
David.

Submitted by babyuniverse on Sat, 2016-07-30 02:52

Will this work in the newer versions of price tapestry?

Submitted by support on Sat, 2016-07-30 08:44

Hi,

Sure - no changes in the relevant database schema but in case of any problems, just let me know what code you're using as there are a number of examples above and i'll check it out..

Cheers,
David.
--
PriceTapestry.com