|
|
Due to the volume of spam happening on our forums, posting is now restricted to verified members only. If you're not verified, drop us a note with your username.
|
|
Home > FlexCMS Support Forum > User Help > Code Snippets > Creating an RSS Feed from your FlexCMS News/Articles
FlexCMS Support Forum
Creating an RSS Feed from your FlexCMS News/Articles Started April 29, 2008 @ 2:46pm by DCSun
|
Post Message |
DCSun Administrator
Posts: 625 |
|
|
Creating an RSS Feed from your FlexCMS News/Articles | April 29, 2008 @ 2:46pm | Hey sports fans, we've had some interest from users to create an RSS feed from their FlexCMS site's news/articles module, so here it is
To make it work, simply create a new page, paste the code below into it, and enable PHP. The address of that page becomes the URL of your feed (ie: http://www.flexcms.com/index.php/pages/rss.html, if you called the page "rss").
Code
if ($IM['na'] == 'y') {
$query_na = "select * from `".$Settings['DBPrefix']."na-Settings`"; $result_na = mysql_query($query_na) or die (mysql_error()); while ($row_na = mysql_fetch_array($result_na)) { $naSettings[$row_na['Name']] = $row_na['Value']; }
$CategoriesList = ""; // optional -- comma separated list of category IDs to use articles from
$ArticlesCount = 5; // optional -- number of articles to include in feed
$DescriptionLength = ""; // optional -- number of characters to use for description, otherwise // defaults to FlexCMS news/articles teaser length setting
header('Content-type: text/xml');
print '<?xml version="1.0" encoding="UTF-8"?> <rss version="0.91"> <channel> <title>'.$Settings['SiteName'].'</title> <link>'.$BaseURL.'</link> <description></description> <language>en-us</language>';
if ($DescriptionLength > 0) { $naSettings['TeaserLength'] = $DescriptionLength; }
if ($ArticlesCount != '') { if (strpos($ArticlesCount,',') !== false) { list($ACOffset,$ACCount) = explode(',',$ArticlesCount); $LimitString = intval($ACOffset).','.intval($ACCount); } else { $LimitString = intval($ArticlesCount); } } else { $LimitString = 5; }
if ($CategoriesList != '') {
$CategoriesArray = explode(',',$CategoriesList); for ($i = 0; $i < count($CategoriesArray); $i++) { $CategoriesArray[$i] = intval($CategoriesArray[$i]); } $CategoriesList2 = '"'.implode('","',$CategoriesArray).'"';
$query_na = "SELECT * FROM `".$Settings['DBPrefix']."na-Articles` WHERE ApprovedBy!='' AND (Category IN (".$CategoriesList2.") OR Category2 IN (".$CategoriesList2.") OR Category3 IN (".$CategoriesList2.") OR Category4 IN (".$CategoriesList2.") OR Category5 IN (".$CategoriesList2.") OR Category6 IN (".$CategoriesList2.") OR Category7 IN (".$CategoriesList2.") OR Category8 IN (".$CategoriesList2.") OR Category9 IN (".$CategoriesList2.") OR Category10 IN (".$CategoriesList2.")) ORDER BY DateCode DESC LIMIT ".$LimitString; } else { $query_na = "SELECT * FROM `".$Settings['DBPrefix']."na-Articles` WHERE ApprovedBy!='' ORDER BY DateCode DESC LIMIT ".$LimitString; }
$result_na = mysql_query($query_na) or die (mysql_error());
$BadWordChars = array( "\xe2\x80\x98", // left single quote "\xe2\x80\x99", // right single quote "\xe2\x80\x9c", // left double quote "\xe2\x80\x9d", // right double quote "\xe2\x80\x94", // em dash "\xe2\x80\xa6", // elipses "\xe2\x80\x93", // long dash "\xe2\x80\x94", // long dash "\xe2\x80\x98", // single quote opening "\xe2\x80\x99", // single quote closing "\xe2\x80\x9c", // double quote opening "\xe2\x80\x9d", // double quote closing "\xe2\x80\xa2", // dot used for bullet points chr(146), );
$FixedWordChars = array( "‘", "’", '“', '”', '—', '…', '-', '-', '\'', '\'', '"', '"', '*', '\'' );
while ($row_na = mysql_fetch_array($result_na)) {
if ($row_na['Teaser'] == '') { $row_na['Article'] = strip_tags($row_na['Article']);
if (strlen($row_na['Article']) > $naSettings['TeaserLength']) { $TeaserSection = substr($row_na['Article'],0,$naSettings['TeaserLength']); while (substr($TeaserSection,strlen($TeaserSection)-1,1) != ' ') { $TeaserSection = substr($TeaserSection,0,strlen($TeaserSection)-1); } while (substr($TeaserSection,strlen($TeaserSection)-1,1) == ' ') { $TeaserSection = substr($TeaserSection,0,strlen($TeaserSection)-1); } $TeaserSection .= '...'; } else { $TeaserSection = $row_na['Article']; } } else { $row_na['Teaser'] = strip_tags($row_na['Teaser']);
$TeaserSection = substr($row_na['Teaser'],0,$naSettings['TeaserLength']);
if (strlen($row_na['Teaser']) > strlen($TeaserSection)) { while (substr($TeaserSection,strlen($TeaserSection)-1,1) != ' ') { $TeaserSection = substr($TeaserSection,0,strlen($TeaserSection)-1); } while (substr($TeaserSection,strlen($TeaserSection)-1,1) == ' ') { $TeaserSection = substr($TeaserSection,0,strlen($TeaserSection)-1); } $TeaserSection .= '...'; }
}
$TeaserSection = str_replace($BadWordChars, $FixedWordChars, $TeaserSection);
$TeaserPrint = ''; for ($x = 0; $x < $naSettings['TeaserLength']; $x++) { $CharCode = ord(substr($TeaserSection,$x,1)); if (($CharCode > 31 && $CharCode < 127) || $CharCode == 10 || $CharCode == 13 || $CharCode == 9) { $TeaserPrint .= chr($CharCode); } else { //$TeaserPrint .= ''.$CharCode.';'; } }
$TeaserSection = str_replace($BadWordChars, $FixedWordChars, $TeaserSection);
$TeaserPrint = ''; for ($x = 0; $x < $naSettings['TeaserLength']; $x++) { $CharCode = ord(substr($TeaserSection,$x,1)); if (($CharCode > 31 && $CharCode < 127) || $CharCode == 10 || $CharCode == 13 || $CharCode == 9) { $TeaserPrint .= chr($CharCode); } else { //$TeaserPrint .= ''.$CharCode.';'; } }
$TeaserPrint = str_replace(' ',' ',$TeaserPrint); $TeaserPrint = str_replace('&','&',$TeaserPrint); $TeaserPrint = str_replace('&','&',$TeaserPrint);
$TitleSection = str_replace($BadWordChars, $FixedWordChars, $row_na['Title']);
$TitlePrint = ''; for ($x = 0; $x < strlen($TitleSection); $x++) { $CharCode = ord(substr($TitleSection,$x,1)); if (($CharCode > 31 && $CharCode < 127) || $CharCode == 10 || $CharCode == 13 || $CharCode == 9) { $TitlePrint .= chr($CharCode); } else { //$TitlePrint .= ''.$CharCode.';'; } }
$TitlePrint = str_replace(' ',' ',$TitlePrint); $TitlePrint = str_replace('&','&',$TitlePrint); $TitlePrint = str_replace('&','&',$TitlePrint);
print '<item> <title>'.$TitlePrint.'</title> <link>'.$MainURL.'/articles/view/'.$row_na['RecordNumber'].'.html</link> <description>'.$TeaserPrint.'</description> </item>';
}
print '</channel> </rss>';
}
exit(); |
|
You can also make it appear as a feed for your site in newer browsers by adding this tag to your template or a block on your site:
Code
<link rel="alternate" type="application/rss+xml" title="YourSiteName News Feed [RSS]" href="http://www.yoursitedomain.com/index.php/pages/rss.html"> |
|
FlexCMS v3.2 Has Been Released! |
|
|
|
Last Edit: November 14, 2010 @ 8:16pm by DCSun | |
|
|
|
| |
|
|
error only for this code on page creation | July 24, 2008 @ 11:39pm | hallo! trying out some snippets that may come in handy while i am still running a demo. after making a new page and inserting this code i get: You don't have permission to access /index.php/admin/pages-edit-save on this server.
which is odd because i can make other pages no problem, though i am not sure if i have any php enabled pages
i am still a beginner with php and cannot figure out why this is not a functioning snippet for me. |
|
|
|
|
|
|
| |
DCSun Administrator
Posts: 625 |
|
|
| July 25, 2008 @ 8:30pm | H,
I wasn't able to duplicate your problem. That code works perfectly on the site I tested it on without any error messages saving/creating the page or running the code.
David
FlexCMS v3.2 Has Been Released! |
|
|
|
|
|
|
| |
|
|
rss page | July 26, 2008 @ 4:07am | i still have no idea why the page creation won't work, but I believe that it has to be an issue with my hosts configuration.
I can, however, save this as a page in my flex folder and it works beautifully. (for others: just remember to add the <?php.....?> tags in the saved rss.php if you copy and paste the above code) cheers, heather |
|
|
|
|
|
|
| |
|
|
rss and me getting annoyed | August 4, 2008 @ 2:24am | I did find a thread that discussed the possibility of php fopen problem...but now, unlike my last demo, the rss.php shows nothing when I call it.
i got it to run as a separate file in my demo, but now that i am all in the right place...i get nothing.
i still have the no permission to save thing when I try to run this as a php enabled block or page, which I know you cannot help with...but the fact that the snippet does not run as rss.php?
any help would make me happier |
|
|
|
Last Edit: August 4, 2008 @ 2:51am by vfcweb | |
|
|
|
| |
DCSun Administrator
Posts: 625 |
|
|
| August 4, 2008 @ 1:36pm | Sorry to hear about all the problems you're having. We'll see what we can do to get you up and running.
First off, stupid questions that need to be asked lol. Is FlexCMS installed there, and is the rest of it working fine? Any errors anywhere else in it? Did you have to do anything special to make it work during the installation process?
If FlexCMS does work fine, it really starts to get on your nerves, doesn't it? We've all been there and done that, where everything almost works except for some stupid little thing that you spend hours of hair pulling trying to make it work. For me this week it was forgetting to Globalize a variable, I think I spent about three hours tearing code apart looking for that!
There's nothing really in that code that should be causing you problems, it doesn't open any files or connect to any sockets, nothing like that. So it sounds like it's more a PHP problem than that script. When you go to include it from a file, have you added <?php to the start and ?> to the end? Are you including it by its full URL like "http://yadayada/scripts/file.php", or locally like "./scripts/file.php"? Have you checked the permissions on that file to make sure they match your FlexCMS index.php?
And regarding the unable to save certain pages, we have seen that before once on a restrictive hosting company. There was a certain combination of characters that would prevent it from going through, for whatever reason. Where is this site hosted?
That's about the best I can do for suggestions right now, but be sure to let us know if it's still not working and we'll see what else we can do.
David
FlexCMS v3.2 Has Been Released! |
|
|
|
Last Edit: August 4, 2008 @ 1:40pm by DCSun | |
|
|
|
| |
|
|
I have bad experience with that code | August 26, 2008 @ 10:37am | Hello, I simply copy and paste you sample code above but the following message appear: -------------------------- XML Parsing Error: xml declaration not at start of external entity Location: http://mydomain/index.php/pages/rss.html Line Number 2, Column 1:<?xml version="1.0" encoding="UTF-8"?> ^ -------------------------- Can you please explain how could this happen? can you post the finalize code. Thanks |
|
|
|
Last Edit: August 26, 2008 @ 10:38am by jpencoder | |
|
|
|
| |
DCSun Administrator
Posts: 625 |
|
|
| August 26, 2008 @ 1:11pm |
Quote (jpencoder)
Hello, I simply copy and paste you sample code above but the following message appear: -------------------------- XML Parsing Error: xml declaration not at start of external entity Location: http://mydomain/index.php/pages/rss.html Line Number 2, Column 1:<?xml version="1.0" encoding="UTF-8"?> ^ -------------------------- Can you please explain how could this happen? can you post the finalize code. Thanks |
|
The code you see above in the first post is the final code, and it works well on a number of other sites. You can see it in action here: http://www.secwb.com/index.php/pages/rss.html.
My best guess is you have something above the tag shown in your error message, either text or maybe just a line break. But without seeing it that's about the best I can come up with.
David
FlexCMS v3.2 Has Been Released! |
|
|
|
|
|
|
| |
DCSun Administrator
Posts: 625 |
|
|
| November 14, 2010 @ 8:18pm | The code in the opening message of this thread has been updated to fix a small bug that caused problems if you had HTML code in the article and it was shorter than the maximum teaser length. To use the new code simply swap out the code in your existing RSS page. Don't forget to save a copy if you've made any changes to yours.
David
FlexCMS v3.2 Has Been Released! |
|
|
|
|
|
|
|
|
|
| MEMBERS
|
|
|