1

I have a set of articles in database I want to add their content to a file located in my project named rss.xml using the xml format. This is the xml file from https://developers.facebook.com/docs/instant-articles/publishing/setup-rss-feed.

<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>News Publisher</title>
    <link>http://www.example.com/</link>
    <description>
      Read our awesome news, every day.
    </description>
    <language>en-us</language>
    <lastBuildDate>2014-12-11T04:44:16Z</lastBuildDate>
    <item>
      <title>This is an Instant Article</title>
      <link>http://example.com/article.html</link>
      <guid>2fd4e1c67a2d28fced849ee1bb76e7391b93eb12</guid>
      <pubDate>2014-12-11T04:44:16Z</pubDate>
      <author>Mr. Author</author>
      <description>This is my first Instant Article. How awesome is this?</description>
      <content:encoded>

        <!doctype html>
        <html lang="en" prefix="op: http://media.facebook.com/op#">
          <head>
            <meta charset="utf-8">
            <link rel="canonical" href="http://example.com/article.html">
            <meta property="op:markup_version" content="v1.0">
          </head>
          <body>
            <article>
              <header>
                <!— Article header goes here -->
              </header>

              <!— Article body goes here -->

              <footer>
                <!— Article footer goes here -->
              </footer>
            </article>
          </body>
        </html>

      </content:encoded>
    </item>
  </channel>
</rss>

This is how far I got in my php:

$crud = new ArticleController();
$file = 'rss.xml'; //open the file
$xml = simplexml_load_file($file);
$channel = $xml->channel; //get channel to add item to
$list=$crud->getAll();  //Returns all articles in database
$item = $channel->addChild('item');
$item->addChild('title', 'a gallery');
$item->addChild('pubDate', '12/12/2017');
$item->addChild('description', 'something');
$content = $item->addChild('content:encoded');
$html = $content->addChild('html');
$xml->asXML($file); //write to file

I'm not going far since my code is returning already a lot of warnings and errors such :

Warning: simplexml_load_file(): rss.xml:25: parser error : Opening and ending tag mismatch: meta line 24 and head in file.php on line 153

Fatal error: Call to a member function children() on a non-object in /var/www/html/pfe2017/controller/ArticleController.php on line 156

Can anyone please help explaining how to accomplish the desired outcome with providing examples?

2
  • Can you add the contents of articalController file Commented May 19, 2017 at 15:33
  • getAll(); will return data from database no need to know what kind of content. No need to know what kind of data it returns, just consider it has two fields "title" and "content" Commented May 19, 2017 at 15:34

2 Answers 2

2

According to RSS Feeds for Instant Articles:

Remember to escape all HTML content by wrapping it within a CDATA section.

So, just wrap the HTML content of content:encoded with <![CDATA[ and ]]>:

<content:encoded><![CDATA[
    <!doctype html>
    ...
    </html>
]]></content:encoded>

Also:

$content = $item->addChild('content:encoded');
$html = $content->addChild('html');

The code above produces the following XML: <encoded><html/></encoded>

Change those lines with something like these:

$content = $item->addChild('content:encoded', null, 'http://purl.org/rss/1.0/modules/content/');
$base = dom_import_simplexml($content);
$docOwner = $base->ownerDocument;
$base->appendChild($docOwner->createCDATASection('<html>Some HTML</html>'));

to produce the following valid XML element:

<content:encoded><![CDATA[<html>Some HTML</html>]]></content:encoded>

For a reference, please, have a look at:

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

4 Comments

Thanks for the tip, although I'm having error here $item = $channel->addChild('item'); Item tag is already a problem.
Ok. Then please update Your question with the CDATA section, and check the error again.
Updated my answer
Hey @Joe I found a solution, I just answered my self +1 for your contribution and tips.
1

I solved it my self here is the code below:

    $rssfeed = '<?xml version="1.0" encoding="ISO-8859-1"?>';
    $rssfeed .= '<rss version="2.0">';
    $rssfeed .= '<channel>';
    $rssfeed .= '<title>My RSS feed</title>';
    $rssfeed .= '<link>my link</link>';
    $rssfeed .= '<description>something</description>';
    $rssfeed .= '<language>en-us</language>';
    $rssfeed .= '<copyright>Copyright (C) 2017</copyright>';

    foreach ($list as $l) {
        $rssfeed .= '<item>';
        $rssfeed .= '<title>' . $l['titre'] . '</title>';
        $rssfeed .= '<description>' . $l['contenu'] . '</description>';
        $rssfeed .= '<link>' . "my link" . '</link>';
        $rssfeed .= '<pubDate>' . date("D, d M Y H:i:s O", strtotime($l['date_de_creation'])) . '</pubDate>';
        $rssfeed .= '</item>';
    }
    $rssfeed .= '</channel>';
    $rssfeed .= '</rss>';
    $handle = fopen("../rss.xml", "w+");
    fclose($handle);
    $myfile = fopen("../rss.xml", "w");
    fwrite($myfile, $rssfeed);
    fclose($myfile);

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.