OZONE Asylum
Forums
XML - XSL - XSLT - XHTML - CSS - DOM
Creating RSS Feed with PHP?
This page's ID:
26850
Search
QuickChanges
Forums
FAQ
Archives
Register
Edit Post
Who can edit a post?
The poster and administrators may edit a post. The poster can only edit it for a short while after the initial post.
Your User Name:
Your Password:
Login Options:
Remember Me On This Computer
Your Text:
Insert Slimies »
Insert UBB Code »
Close
Last Tag
|
All Tags
UBB Help
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> [/code] It's more than a little sloppy, but it gets the job done! /* Sure, go ahead and code in your fancy IDE. Just remember: it's all fun and games until someone puts an $i out */ [small](Edited by [internallink=2459]norm[/internallink] on 10-20-2005 20:48)[/small]
Loading...
Options:
Enable Slimies
Enable Linkwords
« Backwards
—
Onwards »