Google Chrome rendering XML as text for RSS feed - php

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://".

Related

add RSS feed dynamically using 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);

Will string concat case performant issue in PHP?

I see some code like this in PHP
public function fetch()
{
$xml = '';
$xml .= '<' . '?xml version="1.0" encoding="utf-8"?' . '>' . "\n";
$xml .= '<rss version="2.0">';
$xml .= '<channel>';
$xml .= '<title>TEST</title>';
$xml .= '<description>TEST</description>';
foreach ($this->items as $item)
{
$xml .= '<item>';
$xml .= '<title>' . $item['title'] . '</title>';
$xml .= '<description>' . $item['body'] . '</description>';
$xml .= '</item>';
}
$xml .= '</channel>';
$xml .= '</rss>';
return $xml;
}
The code use a lots of stings concat (.=), do you think it is not a good way to do it ?
It look like will case unnecessary memory usage for me.
Those code was a part of "VIEW' of MCV, the function has already get the processed data in a array of "items". And the function is going to make the render.
Will you agree instead of
echo $this->fetch();
using a template file is better ? like this :
include('template.php');
and then in template.php :
<?xml version="1.0" encoding="utf-8">
<rss version="2.0">
<channel>
<title>TEST</title>
<description>TEST</description>
<?
foreach ($this->items as $item){
echo '<item>';
echo '<title>' . $item['title'] . '</title>';
echo '<description>' . $item['body'] . '</description>';
echo '</item>';
}
?>
</channel>
</rss>
I think the second approach would be better. Do you agree? or any other comment ?
Edit :
A user point out that using a template has it's lack too. So, when will you using the the template and when will not ? (those tutorials just always tell me i can use template anyways, i got a little confuse.)
Using a template is not necessarily better since it is an I/O operation (and I/O operation are often costly).
The concatenation in comparison is several times more performant. You could either remove the unnecessary concatenations (like the '<' . '?xml' part) or use HEREDOC / NOWDOC (http://php.net/manual/fr/language.types.string.php).
At the end of the day, it's mostly up to the developper's choice as long as the application is not purely performance-driven.
EDIT: as stated in comments, using an XML reader / writer class would prove more robust and increase maintainability.

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;

PhP not exporting XML in a proper format

Not sure if this is causing my problem but I am trying to setup a rss campaign with Mailchimp. I have several of them setup which work fine.
This time I am trying to create another rss driven campaign, but I am creating the feed myself to get data out of mysql. The only thing is that the XML is exported as plain text and not formatting properly as an xml documnent.
My php code
<?php
header("Content-Type: application/rss+xml; charset=ISO-8859-1");
DEFINE ('DB_USER', '********');
DEFINE ('DB_PASSWORD', '*********');
DEFINE ('DB_HOST', '*******************************');
DEFINE ('DB_NAME', '******************************');
$rssfeed = '<?xml version="1.0" encoding="UTF-8"?>';
$rssfeed .= '<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">';
$rssfeed .= '<channel>';
$rssfeed .= '<title>Free Scrabble Dictionary - Word Of The Day</title>';
$rssfeed .= '<link>http://www.freescrabbledictionary.com</link>';
$rssfeed .= '<description>This is an example RSS feed</description>';
$rssfeed .= '<language>en-us</language>';
$rssfeed .= '<copyright>Copyright (C) '.date(Y).' freescrabbledictionary.com</copyright>';
$connection = #mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
or die('Could not connect to database');
mysql_select_db(DB_NAME)
or die ('Could not select database');
$query = "SELECT * FROM wordday LEFT JOIN wn_synset ON wordday.synset_id = wn_synset.synset_id LEFT JOIN wn_gloss ON wordday.synset_id = wn_gloss.synset_id ORDER BY wordoftheday.ID DESC LIMIT 1";
$result = mysql_query($query) or die ("Could not execute query");
while($row = mysql_fetch_array($result)) {
extract($row);
if ($ss_type == 'n') {
$ss_type = 'Noun';
} elseif ($ss_type == 'v') {
$ss_type == 'Verb';
}
$rssfeed .= '<item>';
$rssfeed .= '<title>'.$word.' - ['.$ss_type.']</title>';
$rssfeed .= '<description>'.$gloss.'</description>';
$rssfeed .= '<link>http://www.freescrabbledictionary.com/dictionary/word/test/</link>';
$rssfeed .= '<pubDate>' . date("D, d M Y H:i:s O", time()) . '</pubDate>';
$rssfeed .= '</item>';
}
$rssfeed .= '</channel>';
$rssfeed .= '</rss>';
echo $rssfeed;
?>
You can see my export at http://www.freescrabbledictionary.com/includes/wordoftheday.php
The php script is producing the correct data I need, but when I run a test email from Mailchimp the fields for pulling the title, description and link are showing as blank. In my mailchimp campaign I am listing the fields as
*|RSSFEED:TITLE|*
*|RSSFEED:DESCRIPTION|*
*|RSSFEED:LINK|*
Can I get my XML to format properly with a .php extension?
Any mailchimp experts out there? :)
Thanks you rock!

XML Parsing Error: not well-formed, opening xml tags

This snippet of code use to work perfectly, nothing has changed except now it's giving off errors.
function wrapOutput($str){
header('content-type: text/xml; charset: utf-8');
$o = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
$o .= ' <rss version="2.0"
xmlns:media="http://search.yahoo.com/mrss/"
xmlns:amp="http://www.adobe.com/amp/1.0">' . PHP_EOL;
$o .= ' <channel>' . PHP_EOL;
$o .= $str;
$o .= ' </channel>' . PHP_EOL;
$o .= ' </rss>' . PHP_EOL;
return $o;
}
Something is going wrong and turning the xml tags into <'xml version="1.0" encoding="UTF-8"'>
Which results into the following error
XML Parsing Error: not well-formed
Location: http://localhost/mrss.php?feed=test
Line Number 1, Column 2:<'xml version="1.0" encoding="UTF-8"'>
-^
I think the problem is de quotes, beacause XML read like eval(), sou u can try escape \"\" quotes

Categories