Topic: New RSS genereating script

I combined two scripts posted on this forum, one for generating googlemap and the other for generating RSS feed, and obtained a new script which works better for me than the old one.
Here it is:

<?
session_start&#40;&#41;; 
include_once&#40;"admin/inc.php"&#41;;
if &#40;$validation == 1&#41; &#123; $val_string = " AND valid = 1"; &#125; else &#123; $val_string = ""; &#125; 
if &#40;$validation == 1&#41; &#123; $val_string2 = " where valid = 1"; &#125; else &#123; $val_string2 = ""; &#125; 

$sql_links = "select $ads_tbl.siteid,$ads_tbl.sitetitle,$ads_tbl.sitedescription,$ads_tbl.datestamp, $cat_tbl.catname from $ads_tbl inner join $cat_tbl on $ads_tbl.sitecatid = $cat_tbl.catid $val_string2 order by datestamp desc, siteid desc limit 20"; 
$sql_result = mysql_query &#40;$sql_links&#41;; 
$num_links = mysql_num_rows&#40;$sql_result&#41;;

$fp = fopen&#40;"rss.xml","w"&#41;; 
fwrite&#40;$fp, "n<rss version="2.0">n"&#41;; 
fwrite&#40;$fp, "<channel>n"&#41;;
fwrite&#40;$fp, "<title>Title</title>n"&#41;;
fwrite&#40;$fp, "<link>http&#58;//".$url."/</link>n"&#41;;
fwrite&#40;$fp, "<description>description</description>n"&#41;;
fwrite&#40;$fp, "<language>en</language>n"&#41;;
fwrite&#40;$fp, "<lastBuildDate>".strftime&#40;"%a, %d %b %Y %T %Z",time&#40;&#41;&#41;."</lastBuildDate>n"&#41;;
fclose&#40;$fp&#41;;


for &#40;$i=0; $i<$num_links; $i++&#41; 
&#123; 
   $sitetitle = ""; 
      $row = mysql_fetch_array&#40;$sql_result&#41;; 
      $siteid = $row&#91;"siteid"&#93;; 
      $sitetitle = strip_tags&#40;$row&#91;"sitetitle"&#93;&#41;; 
      // only displaying first 150 characters of the ad text 
      $sitedescription = substr&#40;$row&#91;"sitedescription"&#93;,0,250&#41;; 
      $catname = $row&#91;"catname"&#93;; 
      //$datestamp = strftime&#40;"%a, %d %b %Y %T %Z",$row&#91;"datestamp"&#93;&#41;; 
      
      $datestamp = strftime&#40;"%a, %d %b %Y %T %Z",strtotime&#40;$row&#91;"datestamp"&#93;&#41;&#41;;


$fp = fopen&#40;"rss.xml","a"&#41;; 
fwrite&#40;$fp, "<item>"&#41;; 
fwrite&#40;$fp, "<title>".$catname." &#58; ".$sitetitle."</title>n"&#41;; 
fwrite&#40;$fp, "<link>http&#58;//".$url."/detail.php?siteid=".$siteid."</link>n"&#41;; 
fwrite&#40;$fp, "<description>".$sitedescription."</description>n"&#41;; 
fwrite&#40;$fp, "<pubDate>".$datestamp."</pubDate>n"&#41;;
fwrite&#40;$fp, "</item>"&#41;; 
fclose&#40;$fp&#41;;
&#125;


$fp = fopen&#40;"rss.xml","a"&#41;; 
fwrite&#40;$fp, "n</channel>n"&#41;;
fwrite&#40;$fp, "n</rss>n"&#41;;
fclose&#40;$fp&#41;;
?>

You first need to create the file rss.xml and Chmod it to 666.
The above code can be put into  a .php file and runed by a cron job (this is what I did so the feed updates regularly).

The script might have some code that does nothing (unfortunately I'm not that good in php to find it and delete it) so anyone who can improve it, is welcomed.

Thumbs up Thumbs down

Re: New RSS genereating script

Dear Atheas,

This is great, thank you! I have a problem though. Get the following error:

Parse error: parse error, unexpected T_STRING in /home/site/rsslinks.php on line 25

I have the rss.xml file in the same folder.

Would love to make this work.

Any ideas what is wrong?

Sincere regards,
Lilian

Re: New RSS genereating script

In my php file the 25th line is:

      $row = mysql_fetch_array&#40;$sql_result&#41;;

and it works

Can you check please if in your file is the same?

Thumbs up Thumbs down

Re: New RSS genereating script

thank you Atheas for your reply. Yes, the line is there:

for ($i=0; $i<$num_links; $i++)
{

Re: New RSS genereating script

The file rss.xml should be empty.
I don't understand why you get an error on line 25.
I copied the code exactly as it is posted on the forum put it into a test.php and then run it through Internet explorer. I get no error.

Maybe you should copy the script again from the forum, maybe the first time something got mixed.

Thumbs up Thumbs down

Re: New RSS genereating script

thank you again. I've re-copied and tried, does not work.

I wonder what the problem is ...

at a loss here ... help please, would love to make this work.

Sincere regards,
Lilian

Re: New RSS genereating script

PM me one of you email adresses and I will send you my test.php file to try.

Thumbs up Thumbs down

Re: New RSS genereating script

Thank you so much Athea, it works beautifully!

What I was supposed to do is look at the feed at rss.xml.

Great support on this forum.

Thank you Are.

Wishing all Happy Holidays!

Lilian

Re: New RSS genereating script

does this show only the overall latest posts?

or can it be modified to show the latest posts from a specific category?

Thumbs up Thumbs down

Re: New RSS genereating script

Atheas email to me mentioned:

By the way, you should change the Title and the description in the script to match your site; you can also increase or decrease the number of Items by modifing in the script "siteid desc limit 20" to for example "siteid desc limit 50" or how ever you want it.

It's beautiful.

Re: New RSS genereating script

how can i do a cron job?

Thumbs up Thumbs down

Re: New RSS genereating script

is it possible to specify a certain category?

Thumbs up Thumbs down

Re: New RSS genereating script

Hi Revan, I'm setting up cron jobs from Cpanel so if you use Cpanel also I can guide you.

rlynch? for your question I don't have an answer yet; I don't know SQL but I'll try to figure out a way. Anyone who knows SQL around here?

Thumbs up Thumbs down

Re: New RSS genereating script

i think rss feeds are a very important feature that will benefit users and drastically increase the opportunity to maximize traffic

together, we should be able to come up with a solution

atheas has supplied a very good base code for us to improve

are? anyone?

Thumbs up Thumbs down

Re: New RSS genereating script

is re-writing to the same file the most efficient way to do it?

what happens if 2 users request the rss (for different categories) at once?

this cant be how yahoo and the like do it

Thumbs up Thumbs down

Re: New RSS genereating script

how do you get the rss to appear like this:

http://rss.news.yahoo.com/rss/us

opposed to the uncollapsable text that it currently shows

Thumbs up Thumbs down

Re: New RSS genereating script

Well my rss.xml page looks as http://rss.news.yahoo.com/rss/us, it has collapsable text. Can you PM me the link to your rss.xml to see how it looks and maybe figure out why it does not have collapsable text?

Thumbs up Thumbs down

Re: New RSS genereating script

nevermind i got it, thanks though

it was because i changed the filename to .rss

however, it should still be collapsable with a .rss extention right?

has anyone tried to add the feed to google homepage?

i get an error whenever i try to add it

also, when creating the xml file, i sometimes get errors whenever a ' (') is used.

anyone else?

Thumbs up Thumbs down

Re: New RSS genereating script

Hi!

The rss.xml work wonderfully, but have a little problem with a 2nd version.

I've got 2 versions of Classifieds running on the same sql database, and for the 2nd version have renamed the tables.

so where previously the table was named ad, now it's ad2, and category is category2.

I'm trying to make the following work:

$sql_links = "select $ads_tbl.siteid,$ads_tbl.sitetitle,$ads_tbl.sitedescription,$ads_tbl.datestamp, $cat_tbl.catname from $ads_tbl inner join $cat_tbl on $ads_tbl.sitecatid = $cat_tbl.catid $val_string2 order by datestamp desc, siteid desc limit 20";

with the names ad2 and category2. Have tried several versions i.e. ($ads2_tbl.siteid), but get error :

line 1, column 0: XML parsing error: <unknown>:1:0: no element found

in the feed validator.

What am I missing here, or obviously doing wrong?

Look forward to hearing from you.

Sincere regards,
Lilian

20

Re: New RSS genereating script

There is an error in the original posted script:

fwrite($fp, "n<rss version="2.0">n");

Needs to be:

fwrite($fp, "n<rss version=\"2.0\">\n");

Also, all n should be \n. Maybe this board program screwed it up.

Re: New RSS genereating script

Here the 100% working script
with tanks on ARe


<?
session_start();
include_once("admin/inc.php");
if ($validation == 1) { $val_string = " AND valid = 1"; } else { $val_string = ""; }
if ($validation == 1) { $val_string2 = " where valid = 1"; } else { $val_string2 = ""; }

$sql_links = "select $ads_tbl.siteid,$ads_tbl.sitetitle,$ads_tbl.sitedescription,$ads_tbl.datestamp, $cat_tbl.catname from $ads_tbl inner join $cat_tbl on $ads_tbl.sitecatid = $cat_tbl.catid $val_string2 order by datestamp desc, siteid desc limit 20";
$sql_result = mysql_query ($sql_links);
$num_links = mysql_num_rows($sql_result);

$fp = fopen("rss.xml","w");
fwrite($fp, "\n<rss version=\"2.0\">\n");
fwrite($fp, "<channel>\n");
fwrite($fp, "<title>Title</title>\n");
fwrite($fp, "<link>http://".$url."/</link>\n");
fwrite($fp, "<description>description</description>\n");
fwrite($fp, "<language>nl</language>\n");
fwrite($fp, "<lastBuildDate>".strftime("%a, %d %b %Y %T %Z",time())."</lastBuildDate>\n");
fclose($fp);


for ($i=0; $i<$num_links; $i++)
{
   $sitetitle = "";
      $row = mysql_fetch_array($sql_result);
      $siteid = $row["siteid"];
      $sitetitle = strip_tags($row["sitetitle"]);
      // only displaying first 150 characters of the ad text
      $sitedescription = substr($row["sitedescription"],0,250);
      $catname = $row["catname"];
      //$datestamp = strftime("%a, %d %b %Y %T %Z",$row["datestamp"]);
     
      $datestamp = strftime("%a, %d %b %Y %T %Z",strtotime($row["datestamp"]));


$fp = fopen("rss.xml","a");
fwrite($fp, "<item>");
fwrite($fp, "<title>".$catname." : ".$sitetitle."</title>\n");
fwrite($fp, "<link>http://".$url."/detail.php?siteid=".$siteid."</link>\n");
fwrite($fp, "<description>".$sitedescription."</description>\n");
fwrite($fp, "<pubDate>".$datestamp."</pubDate>\n");
fwrite($fp, "</item>");
fclose($fp);
}


$fp = fopen("rss.xml","a");
fwrite($fp, "\n</channel>\n");
fwrite($fp, "\n</rss>\n");
fclose($fp);
?>

Thumbs up Thumbs down

Re: New RSS genereating script

what happens if 2 ppl request a rss feed at once?

Thumbs up Thumbs down

Re: New RSS genereating script

Nothing worrisome should happen. 2 people don't access the script directly but access the rss.xml file generated by the script. You can create a cron job to periodically run the script to generate rss.xml. Then this is no different than you index.html file accessed by two people simultaneously (which happens all the time)

Thumbs up Thumbs down

Re: New RSS genereating script

can it be set to make rss feeds for each category?

Thumbs up Thumbs down

Re: New RSS genereating script

I don't how to do this, but I think having rss feed for each catagory will be really a good idea. More and more people prefer rss feed to newsletter so this feature will be in demand some day if not now.

Thumbs up Thumbs down