Topic: Creating RSS Feed with PHP? |
|
---|---|
Author | Thread |
Maniac (V) Inmate From: there...no..there..... |
posted 10-14-2005 17:53
I have been doing some searching on how to do this but I have only found stuff on parsing the XML document with PHP. |
Maniac (V) Mad Scientist From: 100101010011 <-- right about here |
posted 10-14-2005 18:17
RSS is simply an XML format so basically you just need to figure out the format and then get PHP to output the correct XML. You can do this as simple as just dumping XML just like you would HTML (which is probably the easiest method). When doing this you just want to send the xml header at the beginning (Not enitrely necessary but good for some browsers. |
Maniac (V) Inmate From: there...no..there..... |
posted 10-14-2005 18:34
Yeah....I kind of knew that RSS feeds were just XML but couldn't really find anything on how to get the items to update or just pull in all the stuff from the site. quote:
|
Maniac (V) Mad Scientist From: :morF |
posted 10-15-2005 02:56
I, personally, would do this by hand too... such things can be outputted through PHP with ease anyway, so why not go the whole hog? |
Maniac (V) Inmate From: there...no..there..... |
posted 10-16-2005 00:26
wasn't that hard really to write out the XML file itself. Might do some expermenting with XSLT just for goofing off quote:
|
Maniac (V) Mad Scientist From: :morF |
posted 10-16-2005 07:01
PHP will do the writing. You'll use PHP to assemble the structure of the file, then write it out to another file for storage and to allow people to grab it and use it in their news aggregators. |
Maniac (V) Inmate From: there...no..there..... |
posted 10-16-2005 17:59
well I was just going to do it kind of like what sitepoint does. |
Maniac (V) Inmate From: Cell 53, East Wing |
posted 10-17-2005 15:43
#cough# |
Paranoid (IV) Inmate From: 127.0.0.1 |
posted 10-17-2005 18:13
It's actually fairly easy. I've done this, so anyone can! :-) |
Maniac (V) Inmate From: there...no..there..... |
posted 10-17-2005 19:11
groovy stuff. I actually have one that seems to be doing the job. Just need to do some more with the SQL statement and it should be good to go. I just had php spit out the file which works pretty well. Going to go ahead and do some XSLT stuff just to make it look purty. |
Paranoid (IV) Inmate From: [s]underwater[/s] under-snow in Juneau |
posted 10-20-2005 20:43
I wrote this one about 2 years ago to allow administrators to post news to our intranet site and produce an rss feed that could be used if they wanted to provide other departments or outside agencies with syndicated content. code: <?php include("../openNclose.php"); /*------------contents of openNclose------------------------ <?php $open="<?xml version='1.0'?> <rss version='0.91'> <channel>"; $close=" </item> </channel> </rss> "; ?> -----------------------------------------------------------*/ if (isset($_POST['submit'])){ if ($_POST['title2']==""){ echo"<script>alert('Pleases supply a Title'); history.back();</script>"; exit; } if ($_POST['blurb2']==""){ echo"<script>alert('Pleases supply text for your news post'); history.back();</script>"; exit; } $title="StateOfAlaska"; $link="http://state.ak.us"; $blurb=$_POST['blurb']; $title2=$_POST['title2']; $link2=$_POST['link2']; $blurb2=$_POST['blurb2']; $file="../rss/".$title.".rss"; //----------------------------------THIS WILL CHANGE FOR PRODUCTION------------------------------------- $openFile=fopen($file,'w'); $replace="$open"; $replace.=" <title>$title</title> <link>$link</link> <description>$blurb</description> <item> <title>$title2</title> <link>$link2</link> <description>$blurb2</description> "; $replace.="$close"; if(fputs($openFile,$replace)){ echo"<script>alert('Your Update has been processed.')</script>"; fclose($openFile); } else{ echo"My bad.."; } echo "<br /><br /><font size ='6' weight='900'>Webmasters:</font> "; echo "<a href='http://intra1.admin.state.ak.us/newIntra/rss/".$_POST['title'].".rss'>"; echo "right-click/save as</a> to download a copy of the xml/rss file if you would like to host a copy "; echo "on your server also.<br /><br />"; $file1="../rss/".$title.".rss"; //----------------------------------THIS WILL CHANGE FOR PRODUCTION------------------------------------- $file2="../rss/main.rss"; //----------------------------------THIS WILL CHANGE FOR PRODUCTION------------------------------------- $myFile=file($file1); $content; for ( $i = 5; $i < count($myFile)-2; $i++ ) { $content.= $myFile[$i]; } $newGet=file($file2); $final=""; for ( $i = 0; $i < count($newGet)-2; $i++ ) { $final.=$newGet[$i]; } $final.="$content"; $final.="</channel> </rss>"; $openFile2=fopen($file2,'w')or die("<font color=red><br>no/open</font>"); if (fputs($openFile2,$final)){ echo "<font color=red><br></font>"; } else{ echo "<font color=red><br>no file created</font>"; } //----------------------------------------------Load the DB with who posted and when function loadDB(){ global $REMOTE_USER; $mysql_id = mysql_connect('localhost', '******', '********'); mysql_select_db('news',$mysql_id); $now= date("lFj,Y"); $query="INSERT into newsposts VALUES('".$REMOTE_USER."','".$_POST['title2']."','".$now."')"; mysql_query($query)or die(mysql_error()); } loadDB(); } //-------------------------------------------------------------- class xItem { var $xTitle; var $xLink; var $xDescription; } // general vars $sTitle = ""; $sLink = ""; $sDescription = ""; $arItems = array(); $itemCount = 0; // ********* Start User-Defined Vars ************ // rss url goes here $uFile="../my.rss"; // descriptions (true or false) goes here $bDesc = true; // font goes here $uFont = "Verdana, Arial, Helvetica, sans-serif"; $uFontSize = "2"; // ********* End User-Defined Vars ************** function startElement($parser, $name, $attrs) { global $curTag; $curTag .= "^$name"; } function endElement($parser, $name) { global $curTag; $caret_pos = strrpos($curTag,'^'); $curTag = substr($curTag,0,$caret_pos); } function characterData($parser, $data) { global $curTag; // get the Channel information first global $sTitle, $sLink, $sDescription; $titleKey = "^RSS^CHANNEL^TITLE"; $linkKey = "^RSS^CHANNEL^LINK"; $descKey = "^RSS^CHANNEL^DESCRIPTION"; if ($curTag == $titleKey) { $sTitle = $data; } elseif ($curTag == $linkKey) { $sLink = $data; } elseif ($curTag == $descKey) { $sDescription = $data; } // now get the items global $arItems, $itemCount; $itemTitleKey = "^RSS^CHANNEL^ITEM^TITLE"; $itemLinkKey = "^RSS^CHANNEL^ITEM^LINK"; $itemDescKey = "^RSS^CHANNEL^ITEM^DESCRIPTION"; if ($curTag == $itemTitleKey) { // make new xItem $arItems[$itemCount] = new xItem(); // set new item object's properties $arItems[$itemCount]->xTitle = $data; } elseif ($curTag == $itemLinkKey) { $arItems[$itemCount]->xLink = $data; } elseif ($curTag == $itemDescKey) { $arItems[$itemCount]->xDescription = $data; // increment item counter $itemCount++; } } // main loop $xml_parser = xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement"); xml_set_character_data_handler($xml_parser, "characterData"); if (!($fp = fopen($uFile,"r"))) { die ("could not open RSS for input"); } while ($data = fread($fp, 4096)) { if (!xml_parse($xml_parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } } xml_parser_free($xml_parser); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Untitled</title> <meta name="generator" content="BBEdit 7.0" /> <script language="Javascript"> <!-- //--> </script> </head> <body> <h2>Post a news article to be displayed on the Intranet.</h2> <hr /> <?php for ($i=0;$i<count($arItems);$i++) { $txItem = $arItems[$i]; ?> <font face = "<?php echo($uFont); ?>" size = "<?php echo($uFontSize); ?>"><a href = "<?php echo($txItem->xLink); ?>"><?php echo($txItem->xTitle); ?></a></font> <br> <?php if ($bDesc) { ?> <font face = "<?php echo($uFont); ?>" size = "<?php echo($uFontSize); ?>"><?php echo ($txItem->xDescription); ?> <br> <?php } echo ("<br>"); } ?> <hr /><br /> <form action="index.php" method="post"onsubmit=""> <!-- <input type="text"name="title" maxlength="100">your dept. <font color="red"><small>* this will not display on the intranet news page</small></font><br /> <input type="text"name="link" maxlength="100">URL for your dept's home page. <font color="red"><small>* this will not display on the intranet news page</small></font><br /><br /> --> <input type="hidden" name="blurb" value="homepage" > <input type="text"name="title2" maxlength="100">title of news article.<br /> <input type="text"name="link2" maxlength="100">news article URL. <font color="red"><small>* if there is no webpage for this news item, leave this field blank.</small></font><br /> <br /> intro. text for article goes in the box below<br /> <textarea name="blurb2" rows="10" cols="40"> </textarea><br /><br /> <input type="submit" name="submit" value="post news "> </form> </body> </html>
|
Maniac (V) Inmate From: there...no..there..... |
posted 10-20-2005 22:25
wow...thanks for that norm. I'll have a look at that when I get back home. This RSS feed was for a forum so the posting was taken care of. |