RSS feeds created in PHP are not updating - php

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;

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

saving an output to .xml format

I a working on a sample project that gives positions of Telecom operators cell towers based on regions on google maps.
I am following the steps from the google maps API how-to
google maps instructions
i am stuck here. I have followed all the instructions and i am also getting XML output on my webpage from the database. Now as per the instructions that i have to follow, i have to save this output to an .XML file to my server for further processing, and adding markers on the google maps. The markers include the Lat - Long values and some information on the cell towers selected: cell-id, Location code and radio type.
I have searched for the solution but i am stuck at - PHP: DOMDocument::save and how to implement in my code.
The code for the same is attached for reference:
function parseToXML($htmlStr) {
$xmlStr = str_replace('<','<',$htmlStr);
$xmlStr = str_replace('>','>',$htmlStr);
$xmlStr = str_replace('"','"',$htmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr = str_replace('&','&',$htmlStr);
return $xmlStr;
}
$t_query = "SELECT `radio_type`, `lac`,`cell-id`,`longitude`,`latitude` FROM `".DBNAME."`.`celltower` WHERE `mcc` = '".$mcc."' AND `mnc` = '".$mnc."'";
$t_query_exec = mysqli_query($con, $t_query);
if($t_num_rows = 0) {
die("Unable to fetch any data..Please try again..!!");
} else {
header("Content-type: text/xml");
//Begin XML file, echo parent node.
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<root>';
echo '<markers>';
//Iterating through rows, and printing XML nodes for each
while($t_var = mysqli_fetch_assoc($t_query_exec)) {
//Add to XML document node.
echo '<marker ';
echo 'radio_type="' .parseToXML($t_var['radio_type']) . '" ';
echo 'lac="' .parseToXML($t_var['lac']) . '" ';
echo 'cell-id="' .parseToXML($t_var['cell-id']) . '" ';
echo 'longitude="' . $t_var['longitude'] . '" ';
echo 'latitude="' .$t_var['latitude'] . '" ';
echo '/>';
}
echo '</markers>';
echo '</root>';
}
i just need to know that how can i save the output to an .XML file
Thanks in advance.
Just put your XML into a variable and save it with file_put_contents()
$xmlString = '<?xml version="1.0" encoding="utf-8"?>';
$xmlString .= '<root>';
$xmlString .= '<markers>';
//Iterating through rows, and printing XML nodes for each
while($t_var = mysqli_fetch_assoc($t_query_exec)) {
//Add to XML document node.
$xmlString .= '<marker ';
$xmlString .= 'radio_type="' .parseToXML($t_var['radio_type']) . '" ';
$xmlString .= 'lac="' .parseToXML($t_var['lac']) . '" ';
$xmlString .= 'cell-id="' .parseToXML($t_var['cell-id']) . '" ';
$xmlString .= 'longitude="' . $t_var['longitude'] . '" ';
$xmlString .= 'latitude="' .$t_var['latitude'] . '" ';
$xmlString .= '/>';
}
$xmlString .= '</markers>';
$xmlString .= '</root>';
file_put_contents('output.xml', $xmlString);

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)

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!

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