add RSS feed dynamically using php - php

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?

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:
How to write CDATA using SimpleXmlElement?
SimpleXMLElement::addChild

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);

Related

How to decode html tags in xml using php

I have tried to decode html tags in xml but the functions of the html tags was not worked.
Here is my code:
<?php
$sample = '<p>&nbsp Sample</p>
<p>Sample 2</p>';
header('Content-type: text/xml');
$output = '<rss version="2.0">';
$output .= '<channel>';
$output = '<description>.utf8_encode(html_entity_decode($sample)).</description>';
echo($output);
$output .= '</channel>';
$output .= '</rss>';
?>
The output was a plain text. The function of <p> tag was not working. and when I remove utf8_encode it error the .
Try this
<?php
$sample = '<p>Sample</p><p>Sample 2</p>';
header('Content-type: text/xml');
$output = '<rss version="2.0">';
$output .= '<channel>';
$output .= '<description>'. strip_tags(utf8_encode(html_entity_decode($sample))).'</description>';
$output .= '</channel>';
$output .= '</rss>';
echo($output);
Result:
<rss version="2.0">
<channel>
<description>
<p>Sample</p>
<p>Sample 2</p>
</description>
</channel>
</rss>
you need to seperate PHP function on string.

Opening and Ending Tag mismatch error while creating Rss

I am new in php nd want to help in generating rss feed. i have a db with name "test123" having fields Id, Name, Address, Designation and Text. Here is my code for rss feed`
<?php
function connect() {
return new PDO('mysql:host=localhost;dbname=test123', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
}
$pdo = connect();
// posts *******************************
$sql = 'SELECT * FROM form ORDER BY id DESC';
$query = $pdo->prepare($sql);
$query->execute();
$rs_post = $query->fetchAll();
// The XML structure
$data = '<?xml version="1.0" encoding="UTF-8" ?>';
$data .= '<rss version="2.0">';
$data .= '<channel>';
$data .= '<title>PACRA Rss Feed</title>';
$data .= '<link>http://www.pacra.com</link>';
$data .= '<description>Pacra Pakistan</description>';
foreach ($rs_post as $row) {
$data .= '<item>';
$data .= '<title>'.$row['Name'].'</title>';
$data .= '<link>'.$row['Address'].'</link>';
$data .= '<description>'.$row['Text'].'</description>';
$data .= '</item>';
}
$data .= '</channel>';
$data .= '</rss> ';
header('Content-Type: application/xml');
echo $data;
?>`
Problem is that when i run that code it show an error message "Opening and Ending Tag mismatch"
Here is the image of error
Image Link
It might be the header, I use the following in my PHP generated RSS feeds: header('Content-type: text/xml; charset=UTF-8');
And escape your <title> and <description> with CDATA.
Did you validate your feed using the RSS validator? https://validator.w3.org/feed/
Also my structure is a bit different:
<?xml version="1.0" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="http://www.website.com" rel="self" type="application/rss+xml" />
<title>RSS Title</title>
<link>http://www.website.com</link>
<description>Description</description>
<language>en</language>
<managingEditor>Name</managingEditor>
<webMaster>mail#mail.com</webMaster>
<item>
<pubDate>Wed, 02 Oct 2015 15:00:00 +0200</pubDate>
<title><![CDATA[Here the title]]></title>
<link>http://www.website.com/page</link>
<guid>http://www.website.com/page</guid>
<description><![CDATA[Here the content]]></description>
</item>
<item>
<pubDate>Wed, 02 Oct 2015 15:00:00 +0200</pubDate>
<title><![CDATA[Here the title2]]></title>
<link>http://www.website.com/page2</link>
<guid>http://www.website.com/page2</guid>
<description><![CDATA[Here the content2]]></description>
</item>
</channel>
</rss>

php auto rss feed filemtime error stat failed

I have a php code that automatically generates an RSS feed XML file. Now I have my pages which are in a folder. I read them out with simple php to generate title, links, description... that all goes fine and the code works great. But when I try to get the last timestamp of the file itself I get errors. I really can't figure out why the code gives errors.
<?php
$rssfeed = "<?xml version='1.0' encoding='ISO-8859-1'?>
<rss version='2.0'>
<channel>
<title>My RSS feed</title>
<link>http://" . $_SERVER['HTTP_HOST'] . "/</link>
<description>This is an example RSS feed</description>
<language>en-us</language>
<copyright>Copyright (C) 2009 mywebsite.com</copyright>
";
$links = scandir('../pages/');
$links = array_diff($links, array('.', '..', 'subpages', 'protected'));
foreach($links as $link){
$descr = file_get_contents('../description/' . $link);
$descr = str_replace(array('\\'), array(''), $descr);
$pub = date ('F d Y H:i:s.', filemtime($link));
$rssfeed .= "<item>
<title>".$link."</title>
<description>".$descr."</description>
<link>http://" . $_SERVER['HTTP_HOST'] . "/index.php?p=".$link."</link>
<pubDate>".$pub."</pubDate>
</item>";
}
$rssfeed .= "</channel></rss>";
file_put_contents("../RSSfeed.xml", $rssfeed, LOCK_EX);
?>
this is the error I get:
Warning: filemtime(): stat failed for 01Home in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\admin\rss.php on line 35
(localhost on a easyphp server for testing)

RSS feeds created in PHP are not updating

So I am trying to write a program that will send an RSS feed to every subsriber when a new data base entry is added. The code I have will give the subscriber all of the current db entries, but will not send new RSS feeds when a new item is entered. Here is my code:
<?php
header("Content-Type: application/rss+xml; charset=UTF8_general_ci");
//mysql connection is here
$rssfeed = '<?xml version="1.0" encoding="ISO-8859-1"?><rss version="2.0"><channel>';
//rss feed data
$rssfeed .= '<title>test</title>
//link is here
<description>test</description>
<language>en-us</language>';
//items
$results = mysql_query("SELECT name FROM test");
while($row = mysql_fetch_array($results)){ //looping through items
$rssfeed .= '<item>
<title>' . $row['name'] . '</title>
<description>test</description>
//link to my website here
<pubDate>' . date("F j, Y, g:i a") . '</pubDate>
</item>';
}
$rssfeed .= '</channel></rss>';
echo $rssfeed;

Google Chrome rendering XML as text for RSS feed

I have this script to generate an XML file for an RSS feed. Works great in every browser except Chrome. Chrome just renders the XML as text. Something to do with header("Content-Type: application/rss+xml; charset=ISO-8859-1"); possibly?
This is the code I'm using:
<?php
$linkUp = "http://localhost/sites/myBlog/";
header("Content-Type: application/rss+xml; charset=ISO-8859-1");
$rssfeed = '<?xml version="1.0" encoding="ISO-8859-1"?>';
$rssfeed .= '<rss version="2.0">';
$rssfeed .= '<channel>';
$rssfeed .= '<title>Mytitle</title>';
$rssfeed .= '<link>' . $linkUp . '</link>';
$rssfeed .= '<description>Mydescription</description>';
$rssfeed .= '<language>en-us</language>';
$rssfeed .= '<copyright>© ' . strftime('%Y') . ' . " " . ' . $linkUp . '</copyright>';
$query = "SELECT * FROM rss";
$result = $db->query($query);
while($row = $db->fetch_array($result)) {
$rssfeed .= '<item>';
$rssfeed .= '<title>' . $row['rss_title'] . '</title>';
$rssfeed .= '<description>' . $row['rss_description'] . '</description>';
$rssfeed .= '<link>' . $row['rss_link'] . '</link>';
$rssfeed .= '<pubDate>' . date("D, d M Y H:i:s O", strtotime($date)) . '</pubDate>';
$rssfeed .= '</item>';
}
$rssfeed .= '</channel>';
$rssfeed .= '</rss>';
echo $rssfeed;
?>
This is a known bug in chrome that has yet to be fixed, chrome does not display xml rss feeds with any formatting whatsoever.
Update: There is now an RSS subscription / reader extension for Chrome.
I had this same problem and I used "application/xml" and it fixed it right up. Chrome doesn't like "application/rss+xml".
Bottom line, RSS support isnt used by "majority" of users, and as such they are only implementing it as an extension, for now. The extension is available here:
RSS SubscriptionExtension
There's a detailed discussion of this on the closing comment for the bug - you can read the developer notes here:
Comment 149
Try changing the header to text/xml and see if it helps:
header("Content-Type: text/xml; charset=ISO-8859-1");
try the chrome extension "XML Tree"
Short answer: add "view-source:{feedurl}"
Note that when the url ends with .xml and is recognized as a feed by chrome, Chrome annoyingly opens a Save File dialog. But many feed urls don't end with an extension (i.e. .xml), such as:
http://feeds.feedburner.com/ScottHanselman
At root, that url is still a regular, xml feed, but for us coders who just want to see the real xml, Chrome and the others in this case show you a human readable display of the feed (very annoying!).
So the answer to both of these problems is contained in the comment above given by Arne Roomann-Kurrik. He should have put it as an answer, because it works!
view-source:http://feeds.feedburner.com/ScottHanselman
You don't even need "http://".

Categories