PhP not exporting XML in a proper format - php

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!

Related

Compress sitemap.xml into sitemap.xml.gz in php making the file damage

I am trying to convert the sitemap.xml into sitemap.xml.gz , The pages I am extracting from the database , it is converting into sitemap.xml , but when I am trying to compress this is not working . this is my code it is making the file damage .
header('content-type: application/x-gzip');
header('Content-Disposition: attachment; filename="sitemap.xml.gz"');
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>';
$xmlString .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
include("admin/functions/dbconfig.php");
$sql = "select * from zz where aa between 1 and 30000";
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($result)){
$url = $row["aa"];
$xmlString .= '<url>';
$xmlString .= '<loc>http://mynewdomain.com/page.php?word='.htmlentities($url).'</loc>';
$xmlString .= '<lastmod>'.date("Y-m-d").'</lastmod>';
$xmlString .= '<changefreq>monthly</changefreq>';
$xmlString .= '<priority>0.5</priority>';
$xmlString .= '</url>';
}
$xmlString .= '</urlset>';
gzwrite("compress", gzencode($xmlString));
gzclose("compress");

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

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;

What is wrong with my RSS Code? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I need a fresh pair of eyes to look at my RSS code because it is outputting a blank page. However, when I run var_dump() I am receiving output. Can you please let me know what is going?
if($_GET['uid'] == ''){
$usrid = $udata['user_name'];
}else{
$usrid = $_GET['uid'];
}
$udata = $userObj->fetchUser(array("user_name"=>$usrid));
$rssfeed = '<?xml version="1.0" encoding="utf-8"?>';
$rssfeed .= '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">';
$rssfeed .= '<channel>';
$rssfeed .= '<title>Brags By '.$udata['full_name'].'</title>';
$rssfeed .= '<link>'.BASE_URL.'</link>';
$rssfeed .= '<description>'.SITE_TITLE2.'Be amazing. Get noticed.</description>';
$rssfeed .= '<language>en-us</language>';
$rssfeed .= '<copyright>Copyright (C) '.date("Y").' ' . SITE_TITLE2 . '</copyright>';
$dbSel = $db->dbh->prepare("SELECT *,UP.id as bragid FROM ".USERS_BRAG." as UP,".USERS." as U where U.id = UP.user_id AND UP.status = :stat AND U.id= :uID ORDER BY UP.id DESC");
$dbSel->execute(array(':stat'=>'1',':uID'=>$udata['id']));
$result=$dbSel->fetchAll(PDO::FETCH_ASSOC);
//
while($row = $result) {
//echo var_dump($row);die;
$img="<img src='".BASE_URL."public/bragimages/thumb/".$row['brag']."'>";
$link=BASE_URL."bragdetails/".$row['bragid'];
$rssfeed .= "<item>";
$rssfeed .= "<title>".$row['brag_desc']."></title>";
$rssfeed .= "<description>".$img."></description>";
$rssfeed .= "<link>".$link."></link>";
$rssfeed .= "<pubDate>".date("D, d M Y H:i:s O", strtotime($row['added_date']))."></pubDate>";
$rssfeed .= "</item>";
}
$rssfeed .= '</channel>';
$rssfeed .= '</rss>';
echo $rssfeed;
Thanks for the help.
use foreach instead
foreach ($result as $row) {
...
}

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