2

I have a old website that generate its own RSS everytime a new post is created. Everything worked when I was on a server with PHP 4 but now that the host change to PHP 5, I always have a "bad formed XML". I was using xml_parser_create() and xml_parse(...) and fwrite(..) to save everything.

Here is the example when saving (I read before save to append old RSS line of course).

function SaveXml()
{
    if (!is_file($this->getFileName()))
    {
        //Création du fichier
        $file_handler = fopen($this->getFileName(),"w");

        fwrite($file_handler,"");

        fclose($file_handler);
    }//Fin du if

    //Header xml version="1.0" encoding="utf-8"
    $strFileData = '<?xml version="1.0" encoding="iso-8859-1" ?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>'.$this->getProjectName().'</title><link>http://www.mywebsite.com</link><description>My description</description><lastBuildDate>' . date("r"). '</lastBuildDate>';

    //Data
    reset($this->arrData);
    foreach($this->arrData as $i => $value)
    {
        $strFileData .= '<item>';
            $strFileData .= '<title>'. $this->GetNews($i,0) . '</title>';
            $strFileData .= '<pubDate>'. $this->GetNews($i,1) . '</pubDate>';
            $strFileData .= '<dc:creator>'. $this->GetNews($i,2) . '</dc:creator>';
            $strFileData .= '<description><![CDATA['. $this->GetNews($i,3) . ']]> </description>';
            $strFileData .= '<link><![CDATA['. $this->GetNews($i,4) . ']]></link>';
            $strFileData .= '<guid>'. $this->GetNews($i,4) . '</guid>';
            //$strFileData .= '<category>'. $this->GetNews($i,5) . '</category>';
            $strFileData .= '<category>Mycategory</category>';
        $strFileData .= '</item>';

    }//Fin du for i


    $strFileData .= '</channel></rss>';



    if (file_exists($this->getFileName()))//Détruit le fichier
        unlink($this->getFileName());


    $file_handler = fopen($this->getFileName(),"w");



    fwrite($file_handler,$strFileData);

    fclose($file_handler);
}//Fin de SaveXml

My question is : how do you create and fill up your RSS in PHP?

2
  • An example of your feed would help greatly. Commented Sep 17, 2008 at 13:02
  • Done, I have put a snippet of my code Commented Sep 17, 2008 at 13:06

6 Answers 6

2

At swcombine.com we use Feedcreator. Use that one and your problem will be gone. :)

Here is the PHP code to use it once installed:

function feed_simnews() {
    $objRSS = new UniversalFeedCreator();
    $objRSS->title = 'My News';
    $objRSS->link = 'http://link.to/news.php';
    $objRSS->description = 'daily news from me';
    $objRSS->xsl = 'http://link.to/feeds/feedxsl.xsl';
    $objRSS->language = 'en';
    $objRSS->copyright = 'Copyright: Mine!';
    $objRSS->webmaster = '[email protected]';
    $objRSS->syndicationURL = 'http://link.to/news/simnews.php';
    $objRSS->ttl = 180;

    $objImage = new FeedImage();
    $objImage->title = 'my logo';
    $objImage->url = 'http://link.to/feeds/logo.jpg';
    $objImage->link = 'http://link.to';
    $objImage->description = 'Feed provided by link.to. Click to visit.';
    $objImage->width = 120;
    $objImage->height = 60;
    $objRSS->image = $objImage;

    //Function retrieving an array of your news from date start to last week
    $colNews = getYourNews(array('start_date' => 'Last week'));

    foreach($colNews as $p) {
        $objItem = new FeedItem();
        $objItem->title = $p->title;
        $objItem->description = $p->body;
        $objItem->link = $p->link;
        $objItem->date = $p->date;
        $objItem->author = $p->author;
        $objItem->guid = $p->guid;

        $objRSS->addItem($objItem);
    }

    $objRSS->saveFeed('RSS2.0', 'http://link.to/feeds/news.xml', false);
};

Quite KISS. :)

Sign up to request clarification or add additional context in comments.

1 Comment

Look pretty interesting, I'll check that solution as soon as I can get off the job!
2

I would use simpleXML to create the required structure and export the XML. Then I'd cache it to disk with file_put_contents().

Comments

1

I've used this LGPL-licensed feedcreator class in the past and it worked quite well for the very simple use I had for it.

1 Comment

I use this as well. Extensible and easy to grok, and makes valid RSS out of the box.
0

Not a full answer, but you don't have to parse your own XML. It will hurt performance and reliability.

But definitely make sure it is well-formed. It shouldn't be very hard if you generate it by hand or using general-purpose tools. Or maybe your included HTML ruins it?

2 Comments

I do add HTML inside the <[Data tag. bad idea? And how can I do not have to parse the XML is I would like to append without erasing old post?
Okay, that shouldn't break well-formedness. Ah, you're parsing XML to modify it... why not generate the whole thing from scratch then? If it's RSS feed you shouldn't have more than 10 posts in it. Even if you have 100 it shouldn't be a problem.
0

There are lots of things that can make XML malformed. It might be a problem with character entities (a '<', '>', or '&' in the data between the XML tags). Try running anything output from a database through htmlentities() when you concatenate the string. Do you have an example of the generated XML for us to look at so we can see where the problem is?

1 Comment

I added an example in the original post for you.
0

PHP5 now comes with the SimpleXML extension, it's a pretty quick way to build valid XML if your needs aren't complicated.

However, the problem you're suggesting doesn't seem to an issue of implementation more a problem of syntax. Perhaps you could update your question with a code example, or, a copy of the XML that is produced.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.