RSS feed issues

3 posts by 2 authors in: Forums > CMS Builder
Last Post: January 4, 2019   (RSS)

Hi all,

I am trying to set up an RSS feed using the cmsb rss feed generator but am running into all sorts of issues.

For a start, if I include the following line 

<?php header('Content-type: application/xml; charset=utf-8'); ?><?php echo '<'.'?xml version="1.0" encoding="UTF-8"?>'; ?>

I get an error "error on line 35 at column 118: Document is empty"

When testing the page with rss viewers there doesn't seem to be any content in the feed yet the foreach routine looks fine to me.  The location of the feed is https://www.bridlington.net/bridlington-news.xml if someone would be kind enough to take a look and advise where the problem lies i would be most grateful.

The code for the page is below if it helps.

Thanks in advance for any help provided

<?php header('Content-type: application/xml; charset=utf-8'); ?>
<?php
  /* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */
  
  // load viewer library
  $libraryPath = 'cmsb/lib/viewer_functions.php';
  $dirsToCheck = array('/home/sites/4b/4/4af2cde633/public_html/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

  // load records from 'current_news'
  list($current_newsRecords, $current_newsMetaData) = getRecords(array(
    'tableName'   => 'current_news',
    'orderBy'     => '',   // use default database order
    'loadUploads' => false,
    'allowSearch' => false,
  ));

?>
<?PHP
function maxWords($textOrHtml, $maxWords) {
$text=str_replace("<p>","*P*",$textOrHtml);
$text= str_replace("</p>","*/P*",$text);
$text = strip_tags($text);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);
$output=str_replace("*P*","<p>",$output);
$output=str_replace("*/P*","</p>",$output);
$output.="...</p>";

return $output;
}
?>
<?php header('Content-type: application/xml; charset=utf-8'); ?><?php echo '<'.'?xml version="1.0" encoding="UTF-8"?>'; ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
	  <atom:link href="https://bridlington.net/bridlington-news.xml" rel="self" type="application/rss+xml" />
    <title>Bridlington news from Bridlington.net</title>
    <link>https://www.bridlington.net/</link>
    <description>Bridlington.net providing Bridlington news and information.</description>
    <pubDate><?php echo date('r') ?></pubDate>
    <language>en-us</language>
	  	


    <?php foreach ($current_newsRecords as $record): ?>
    <item>
      <title><?php echo htmlencode($record['title']) ?></title>
      <link>https://<?php echo $_SERVER['HTTP_HOST']; ?>/<?php echo $record['_link'] ?></link>
      <description>  <![CDATA[<?php echo maxWords ($record['content'],60) ?> <a href="https://<?php echo $_SERVER['HTTP_HOST']; ?><?php echo $record['_link'] ?>" target="_blank">Read more..</a>]]></description>
      <pubDate><?php echo date('r', strtotime($record['createdDate'])) ?></pubDate>
      
    </item>
    <?php endforeach ?>
  </channel>
</rss>

By daniel - January 3, 2019

Hi willydoit,

It looks like the primary issue is that you have saved this code in a .xml file, which the web server is not parsing for PHP code (you can see the un-executed PHP if you view source on the file from a browser). The easiest way to solve this issue is to simply rename the file as .php. The header() function that sets the content type will let the browser know to read it as an XML file, despite the PHP file extension.

Additionally, I see that you have a duplicate header() call at the top of the file; this may also cause some issues, so I'd recommend removing it.

I hope this helps!

Thanks,

Daniel
Technical Lead
interactivetools.com