tags:

views:

392

answers:

6
+2  Q: 

PHP - RSS builder

Hello,

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/"&gt;&lt;channel&gt;&lt;title&gt;'.$this-&gt;getProjectName().'&lt;/title&gt;&lt;link&gt;http://www.mywebsite.com&lt;/link&gt;&lt;description&gt;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  A: 

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

Marc Gear
+1  A: 

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.

hasseg
I use this as well. Extensible and easy to grok, and makes valid RSS out of the box.
cori
A: 

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.

Andrew

Andrew Taylor
A: 

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?

phjr
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?
Daok
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.
phjr
A: 

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?

Jarett
I added an example in the original post for you.
Daok
+1  A: 

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. :)

Veynom
Look pretty interesting, I'll check that solution as soon as I can get off the job!
Daok
It works!!!! Thx Veynom!
Daok