Rss problems

Im trying to create a RSS page but ran into

Heres the script

<?php header('Content-Type: text/xml'); ?>
<?php echo '<?xml version="1.0" encoding="utf-8"?>'; ?>
<rss version="2.0">
  <channel>
    <title>Assignment 9 - Newsfeed</title>
    <link>http://teamluke.net/</link>
    <author>Luke Urtnowski</author>
    <description>The trials & Tribulations of Luke Urtnowski as I complete this assignment.</description>
    <language>en-us</language>

<?php
  require_once('db/conn.php');

          try {
                $stmt = $conn->prepare('SELECT * FROM `products`');
        
                $stmt->execute();
                
                if($stmt->rowCount()) {
                    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
					echo "<Product>\n";
					echo "\t<Name>" . $row['productName'] . "</Name>\n";
					echo "\t<Line>" . $row['productLine'] . "</Line>\n";
					echo "\t<Vendor>" . $row['productVendor'] . "</Vendor>\n";
					echo "\t<Description>" . $row['productDescription'] . "</Description>\n";
					echo "\t<Price>$" .number_format($row['buyPrice'], 2) . "</Price>\n";
					echo "</Product>\n";

                    }
                } else {
                echo '<product>No products found...</priduct>';
                }
          } catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
          }
?>

  </channel>
</rss>

what is the error talking about?

Line 6 is

<description>The trials & Tribulations of Luke Urtnowski as I complete this assignment.</description> 

The ampersand indicates an entity, hence the error message

xmlParseEntityRef

You could try changing “HTML characters” to proper entities eg. &amp;

Though it might be easier / better to wrap content that could break the XML in CDATA eg.

<description><![CDATA[ Trials & Tribulations ]]></description> 
1 Like

thanks, that worked…now im getting

heres the script

<?php header('Content-Type: text/xml'); ?>
<?php echo '<?xml version="1.0" encoding="utf-8"?>'; ?>
<rss version="2.0">
  <channel>
    <title>Assignment  - Newsfeed</title>
    <link>http://teamluke.net/</link>
    <author>Luke Urtnowski</author>
    <description>The trials &amp; Tribulations of Luke Urtnowski as I complete this assignment.</description>
    <language>en-us</language>

<?php
  require_once('db/conn.php');

          try {
                $stmt = $conn->prepare('SELECT productName, productLine, productDescription, productVendor, buyPrice FROM `products`');
        
                $stmt->execute();
                
                if($stmt->rowCount()) {
                    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
					echo "<Product>\n";
					echo "\t<Name>" . $row['productName'] . "</Name>\n";
					echo "\t<Line>" . $row['productLine'] . "</Line>\n";
					echo "\t<Vendor>" . $row['productVendor'] . "</Vendor>\n";
					echo "\t<Description>" . $row['productDescription'] . "</Description>\n";
					echo "\t<Price>$" .number_format($row['buyPrice'], 2) . "</Price>\n";
					echo "</Product>\n";

                    }
                } else {
                echo '<product>No products found...</product>';
                }
          } catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
          }
?>

  </channel>
</rss>

I wrapped the top <description tag like you said, now I get

It seems like im, always getting an encoding error, doesn’t

<?php echo '<?xml version="1.0" encoding="utf-8"?>'; ?>

work/?

That declaration looks OK.

It’s the same problem but in a different place

<Description>Model features 30 windows, skylights & glare resistant glass, working steering system, original logos</Description>

As I posted before.

If you are changing & to &amp; manually you’re going to be chasing down errors way too often.

I guess you could pass text content through a function that does some kind of “replace”.

But IMHO it’s less resource use and a lot easier to put the content inside CDATA in the “template” file that puts the RSS together. eg.

echo "\t<Description><![CDATA[ " . $row['productDescription'] . " ]]></Description>\n"; 
1 Like

this ok? (thought it would be best to use CDATA many places

<?php header('Content-Type: text/xml'); ?>
<?php echo '<?xml version="1.0" encoding="utf-8"?>'; ?>
<rss version="2.0">
  <channel>
    <title><![CDATA[Assignment 9  - Newsfeed]]></title>
    <link>http://teamluke.net/</link>
    <author>Luke Urtnowski</author>
    <description><![CDATA[ The trials & Tribulations of Luke Urtnowski as I complete this assignment.]]></description>
    <language>en-us</language>

<?php
  require_once('db/conn.php');

          try {
                $stmt = $conn->prepare('SELECT productName, productLine, productDescription, productVendor, buyPrice FROM `products`');
        
                $stmt->execute();
                
                if($stmt->rowCount()) {
                    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
					echo "<Product>";
					echo "  <Name><![CDATA[" . $row['productName'] . "]]></Name>";
					echo "  <Line><![CDATA[" . $row['productLine'] . "]]></Line>";
					echo "  <Vendor><![CDATA[" . $row['productVendor'] . "]]></Vendor>";
					echo "  <Description><![CDATA[" . $row['productDescription'] . "]]></Description>";
					echo "  <Price><![CDATA[$" .number_format($row['buyPrice'], 2) . "]]></Price>";
					echo "</Product>";

                    }
                } else {
                echo '<Product>No products found...</Product>';
                }
          } catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
          }
?>

  </channel>
</rss>

The Result…

Why am I getting the encoding error? Is’nt all my data wrapped using ?

Ah, this time it is a character encoding issue

https://validator.w3.org/feed/check.cgi?url=http%3A%2F%2Fphp_class.teamluke.net%2FAssignment_9%2Frss.php

http://www.feedvalidator.org/check.cgi?url=http%3A%2F%2Fphp_class.teamluke.net%2FAssignment_9%2Frss.php

1 Like

I think I know the deal, I sort of went maverick and made up tags () but I guess I can only use these tags, https://www.tutorialspoint.com/rss/rss2.0-tag-syntax.htm

You can do that, but they should be namespaced with the namespace in the “rss version” tag. Then you can do like this eg.

<rss version=" ..... 
  xmlns:LU="http://some_URL/" 
..... 
<LU:Products> 

http://www.disobey.com/detergent/2002/extendingrss2/

ok, then
Im still stuck on one problem,
heres my assignment

Your RSS feed is to display these fields:

  • Product Name
  • Product Line
  • Product Scale
  • Product Vendor
  • Product Description
  • Buy Price formatted in US dollars
    The product name of each feed item, is to be a link to a product page. You may choose the format of the remaining item data.

I have most of it down, but looked at the available tags and made some guesses, is this ok?

<?php header('Content-Type: text/xml'); ?>
<?php echo '<?xml version="1.0" encoding="utf-8"?>'; ?>
<rss version='2.0'>
  <channel>
    <title><![CDATA[Assignment 9  - Newsfeed]]></title>
    <link>http://teamluke.net/</link>
    <webMaster>lurtnowski@gmail.com</webMaster>
    <description><![CDATA[The trials & Tribulations of Luke Urtnowski as I complete this assignment.]]></description>
    <language>en-us</language>
      <image>
         <title>Luke Urtnowski</title>
         <url>http://php_class.teamluke.net/Assignment_9/images/luke.jpg</url>
         <link>http://php_class.teamluke.net/Assignment_9/images/luke.jpg</link>
         <description><![CDATA[The trials & Tribulations of Luke Urtnowski as I complete this assignment.]]></description>
      </image>
<?php
  require_once('db/conn.php');

          try {
                $stmt = $conn->prepare('SELECT productCode, productName, productLine, productDescription, productVendor, buyPrice FROM `products` WHERE productLine = "Trains" OR  productLine = "Vintage Cars" ORDER BY timeStamp ASC LIMIT 10');
        
                $stmt->execute();
                
                if($stmt->rowCount()) {
                    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
					echo '<item>';
					echo '  <link>http://php_class.teamluke.net/Assignment_9/product.php?Code='.$row['productCode'].'>';
					echo '  <title>' . $row['productName'] . '</title>';
					echo '  </link>';
					echo '  <category>' . $row['productLine'] . '</category>';
					echo '  <source>' . $row['productVendor'] . '</source>';
					echo '  <description><![CDATA[' . $row['productDescription'] . ']]></description>';
					echo '  <Price>$' .number_format($row['buyPrice'], 2) . '</Price>';
					echo '</item>';
                    }
				}
          } catch(PDOException $e) {
            echo 'ERROR: ' . $e->getMessage();
          }
?>

  </channel>
</rss>

I looked and couldn’t figure out if my code in the while loop makes sence.
Does it to you?

ok, made a few changes and this is where im at.
https://validator.w3.org/feed/check.cgi?url=http%3A%2F%2Fphp_class.teamluke.net%2FAssignment_9%2Frss.php
Every error occurred 10- times so the problem is everything in the while loop

 while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo '<item>';
	echo '  <link>http://php_class.teamluke.net/Assignment_9/product.php?Code='.$row['productCode'].'';
	echo '  <title>' . $row['productName'] . '</title>';
	echo '  </link>';
	echo '  <category>' . $row['productLine'] . '</category>';
	echo '  <source>' . $row['productVendor'] . '</source>';
	echo '  <description><![CDATA[' . $row['productDescription'] . ']]></description>';
	echo '  <Price>$' .number_format($row['buyPrice'], 2) . '</Price>';
	echo '</item>';
}

I sort of guessed on what tags to use (out of the ones I can) and I had no idea how to use a tag to describe the price.
Thanks for your help thus far

Maybe you’re thinking of HTML anchor tags?
Trying to sneak something in instead of using a namespace for you “custom” tags?

AFAIK <title> nested inside of <link> isn’t valid.

There’s nothing wrong with experimenting and trying to do things differently. That;s how progress is made.

But believe me, I do it quite often myself and the vast majority of times it results in FAIL.

If you don’t want to tackle namespaces quite yet, I suggest you comment out the custom tag lines of code for now and see if you can get it to be valid without them first before moving on.

ok, ill create a namespace for those tags

Dang, didn’t know how the RSS worked
(attachment)
When I go to the page and open up the reader, all it shows are the and < link> elements of each of the 10 items. What is the purpose of the other available tags, and why use a namespace?

What is displayed depends on the “reader”

For example, browsers without a “feed reader” will usually display the raw RSS

Compare to Opera that does have a feed reader

Then there are feed readers that work independently of any browser.

What is initially displayed will depend on what is being used as the feed reader.

RSS is a type of XML
The “X” is for “extensible”

In other words, RSS has certain defined tags, but the set of tags can be extended to include “custom” tags.

However, the custom tags need to be defined in a namespace.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.