RSS feed php + mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

RSS feed php + mysql

Post by aravona »

I've been following a tutorial on RSS feeds which utilises php and mysql. It creates a function and there doesnt seem to be any problems with it, at least I'm getting no warnings, notices or fatal errors.

Whats happening though is the following bit of code which is supposed to put the feed on the page is instead just echoing the function name:

Code: Select all

<?
  header("Content-Type: application/xml; charset=ISO-8859-1");
  include("classes/RSSclass.php");
  $rss = new RSS();
  echo $rss->GetFeed();
?>
   
this ends up putting this on the screen

Code: Select all

GetFeed(); ?>
Since its a tutorial and this is the first time I've built my own RSS feed I'm a little stumped.
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Re: RSS feed php + mysql

Post by davex »

Try viewing the source of the page - I think you'll find the source code maybe has some extra data - looks like maybe the opening < is being shown until the > found at the -> class pointer and so the whole lot is being taken as an HTML element.

Also I would try changing the initial line from <? to <?php.

Cheers,

Dave.
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: RSS feed php + mysql

Post by aravona »

Thanks switching all that around got rid of the problem but now my screen shows:

Code: Select all

The XML page cannot be displayed 
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later. 
 
 
--------------------------------------------------------------------------------
 
System error: -2146697204. Error processing resource 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'.
Only I'm not sure what the problem can be, I'm new to RSS feeds like I said.

Code: Select all

<?php
 
  class RSS
  {
    public function RSS()
    {
        require_once ('connection.php');
    }
 
    public function GetFeed()
    {
        return $this->getDetails() . $this->getItems();
    }
 
    private function dbConnect()
    {
        DEFINE ('LINK', mysql_connect (localhost, 'Aravona', 'texass9'));
    }
 
    private function getDetails()
    {
        $detailsTable = "rss_details";
        $this->dbConnect($detailsTable);
        $query = "SELECT * FROM ". $detailsTable;
        $result = mysql_db_query (gametest, $query, LINK);
 
        while($row = mysql_fetch_array($result))
        {
            $details = '<?xml version="1.0" encoding="ISO-8859-1" ?>
                <rss version="2.0">
                    <channel>
                        <title>'. $row['title'] .'</title>
                        <link>'. $row['link'] .'</link>
                        <description>'. $row['description'] .'</description>';
        }
        return $details;
    }
 
    private function getItems()
    {
        $itemsTable = "rss_items";
        $this->dbConnect($itemsTable);
        $query = "SELECT * FROM ". $itemsTable;
        $result = mysql_db_query (gametest, $query, LINK);
        $items = '';
        while($row = mysql_fetch_array($result))
        {
            $items .= '<item>
                <title>'. $row["title"] .'</title>
                <link>'. $row["link"] .'</link>
                <description><![CDATA['. $row["description"] .']]></description>
            </item>';
        }
        $items .= '</channel>
                </rss>';
        return $items;
    }
 
}
 
?>
 
Though I dunno what to do now?
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Re: RSS feed php + mysql

Post by davex »

Hi,

Ok there are a few things with your script - for example it looks like you're outputting the XML header within the while loop.

What browser are you using? (The error is unfamiliar to me).

Anyway - view the source of the page and post the XML that has been output please unless of course the problem is obvious.

Only some browsers will support direct display of XML data correctly including RSS. Firefox does it fine as long as all the schema is correct.

Cheers,

Dave.
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: RSS feed php + mysql

Post by aravona »

I cannot view the souce code if I try it says 'The XML source file is unavailable for viewing' So I guess soemthing is really messed up but I've only worked with plain XML pulled with javascript once or twice.

Like I said this is from a tutorial and there were no complaints with it not working.
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Re: RSS feed php + mysql

Post by davex »

What browser are you using?

So you're pulling the XML data via JavaScript. I think you said it works ok if you view the page directly in the browser rather than pulling it via JavaScript? Can you view the source then?

Cheers,

Dave.
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Re: RSS feed php + mysql

Post by davex »

Hi - try this to see the source output.

Change your index page to the following:

Code: Select all

<?
  //header("Content-Type: application/xml; charset=ISO-8859-1"); // commenting out this line
  include("classes/RSSclass.php");
  $rss = new RSS();
  echo htmlspecialchars($rss->GetFeed()); // wrapping $rss->GetFeed() in htmlspecialchars()
?>
It should output the XML as HTML and you can see it on the browser page.

Cheers,

Dave.
aravona
Forum Contributor
Posts: 347
Joined: Sat Jun 13, 2009 3:59 pm
Location: England

Re: RSS feed php + mysql

Post by aravona »

I'm afraid that didnt work, I may have to try something else out. I do have some access to other RSS feeds so will have to give it another try when I can acces them.

Thanks for helping though :)
Post Reply