I am trying to create a RSS feed. This is the PHP code for the feed in question below.
<?xml version="1.0" encoding="utf-8" ?>
<?php
include_once 'authorization.php';
header("Content-Type: text/xml; charset=UTF-8");
$demo_id = "1";
$stmt = $mysqli->prepare("SELECT * FROM demo_files WHERE demo_id = ?");
$stmt->bind_param('i', $demo_id);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) {
$demo_title=$row['demo_title'];
$demo_content=$row['demo_content'];
echo '<rss version="2.0">
<channel>
<title>Liorall</title>
<link>Youtube Tutorial demo</link>
<language>en-us</language>
<item>
<title>'.$post_title.'</title>
<description>'.$post_content.'</description>
</item>
</channel>
</rss>';
}
?>
Am getting this error Extra content at the end of the document
Please how can i fix it.
Thanks in Advance.
You're initialising $demo_title and $demo_content at the top of your loop, but emitting $post_title and $post_content later.
That will generate an undefined variable warning from PHP that will appear in your output stream and your RSS reader is likely to complain about that.
I changed that on my test system and the output now appears well-formed, but it fails the W3C Validator with some other issues you should address.
Related
I'm working on a very simple RSS Feed. What I am doing is pulling the information from a database and transforming it into XML using PHP. However, when I use Chrome to look at the code to make sure it is all appearing as it should, I get these errors at the top of the page.
Here is the code that I am using to pull from my database and create the RSS Feed.
<?php
include('connectDatabaseScript.php');
$sql = "SELECT * FROM table ORDER BY id DESC";
$query = mysql_query($sql) or die(mysql_error());
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>
<rss version='2.0'>
<channel>
<title>My RSS Feed</title>
<link>http://www.mywebsite.com/rss.php</link>
<description>The description for the feed.</description>
<language>en-us</language>";
while($row = mysql_fetch_array($query)) {
$title=$row['title'];
$finalTitle = str_replace("&", "and", $title);
$link=$row['link'];
$newLink = str_replace("&", "&", $link);
$category = $row['category'];
$date = $row['date'];
$description = $row['description'];
echo "<item>
<title>$finalTitle</title>
<link>$newLink</link>
<description>$description</description>
<author>John Doe</author>
<pubDate>$date<pubDate>
<category>$category</category>
</item>";
}
echo "</channel></rss>";
?>
This code usually gets stuck on the title tag. When it does that, it will merge together the link and can also merge the rest of the item and several others after it. Here is an example of what is happening.
<item>
<title>Title No 415: Title <item>
<title>Title No 291: Another Title</title>
<link>http://www.mywebsite.com/post.php?id=291</link>
<description>description</description>
<author>John Doe</author>
<pubDate>Jan. 1, 2000</pubDate>
<category>Generic</category>
</item>
I have figured out what character is causing this to occur. It is the "–" character that appears in some of the titles that I have that is causing the problem. I've been trying to remove it by using the str_replace function. While I have been able to use it with "&" with success, it is not working with "–". Is there another solution to get rid of the "–" from the title or is it still possible with str_replace?
You should not write your XML like this. To avoid this kind of errors, you may use DOMDocument to write your XML, and save it using saveXML.
I have some PHP scripts that make a MySQL query and use it to produce an RSS feed. The text for RSS elements such as title and description needs to be cleaned up for presentation as XML.
Here's a function to do that:
function clean_text($in_text) {
return utf8_encode(
htmlspecialchars(
stripslashes($in_text)));
}
I think a simpler function might solve the problem you're having:
function clean_text($in_text) {
return htmlspecialchars(
stripslashes($in_text));
}
The call to utf8_encode() encodes an ISO-8859-1 string as UTF-8 and was necessary for me because I was dealing with ISO-8859-1 character encoding in my database. The htmlspecialchars() function in PHP turns & to &, < to < and > to >.
Here's a statement that uses the function to output some RSS:
echo "<description>" . clean_text($row['description']) . "</description>";
I have to following code to upload data to a third party. However when I test it in a validator I get the following error:
Undefined root elements:items and text/xml media type is not specific enough.
I have no clue has what I need to change.
Here is the code:
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>
<items>';
mysql_select_db($database_dconn, $dconn);
$query_feed = "SELECT * FROM dat_table WHERE time BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()";
$feed = mysql_query($query_feed, $dconn) or die(mysql_error());
while ($row_feed = mysql_fetch_assoc($feed)){
echo'<item>
<name>'.$row_feed['Name'].'</name>
<email>'.$row_feed['email'].'</email>
<date>'.$row_feed['Date'].'</date>
<description>'.$row_feed['Make'].' '.$row_feed['Model'].' '.$row_feed['Type'].'</description>
<logon>'.$row_feed['Logon'].'</logon>
<category>'.$row_feed['Type'].'/'.$row_feed['Make'].'</category>
<product_search_code>'.$row_feed['Product_search_code'].'</product_search_code>
<order_ref>'.$row_feed['Invoice'].'</order_ref>
<product_link>'.$row_feed['Product_link'].'</product_link>
<customer_ref>'.$row_feed['Invoice'].'</customer_ref>
<amount>'.$row_feed['Price'].'</amount>
<currency>GBP</currency>
</item>';
}
echo '</items>
';
This is RSS out put in validator:
<?xml version="1.0" encoding="UTF-8"?>
<items><item>
<name>Name of customer</name>
<email>customer#gmail.com</email>
<date>03/04/2014</date>
<description>Roland FP80BK Piano</description>
<logon>www.pianoandkeyboardshoponline.co.uk</logon>
<category>Piano/Roland</category>
<product_search_code>264</product_search_code>
<order_ref>8336</order_ref>
<product_link>http://www.pianoandkeyboardshop.co.uk/Roland-FP80-Digital-Piano-in-Satin-Black/264</product_link>
<customer_ref>8336</customer_ref>
<amount>1500.00</amount>
<currency>GBP</currency>
</item><item>
next item etc
---
--
</item><items>
Any help is really appreciated.
The text/xml content type is for generic XML documents. For a feed, try using
header('Content-Type: application/rss+xml');
But anyway, in order to create a valid RSS you have to use the correct RSS tags (Something like this). You seem to use a lot of custom tags instead, so I don't know what exactly you need to do. The easiest solution would be to use namespaces for your custom tags, so something like:
header('Content-Type: application/rss+xml');
echo '<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:mytags="http://mytags.something.net">
<channel>
<title>My RSS Title</title>
<description>This is the description for my RSS feed</description>
<link>http://www.someexamplerssdomain.com/main.html</link>
<mytags:items>';
mysql_select_db($database_dconn, $dconn);
$query_feed = "SELECT * FROM dat_table WHERE time BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()";
$feed = mysql_query($query_feed, $dconn) or die(mysql_error());
while ($row_feed = mysql_fetch_assoc($feed)){
echo '<mytags:item>
<mytags:name>'.$row_feed['Name'].'</mytags:name>
<mytags:email>'.$row_feed['email'].'</mytags:email>
<mytags:date>'.$row_feed['Date'].'</mytags:date>
<mytags:description>'.$row_feed['Make'].' '.$row_feed['Model'].' '.$row_feed['Type'].'</mytags:description>
<mytags:logon>'.$row_feed['Logon'].'</mytags:logon>
<mytags:category>'.$row_feed['Type'].'/'.$row_feed['Make'].'</mytags:category>
<mytags:product_search_code>'.$row_feed['Product_search_code'].'</mytags:product_search_code>
<mytags:order_ref>'.$row_feed['Invoice'].'</mytags:order_ref>
<mytags:product_link>'.$row_feed['Product_link'].'</mytags:product_link>
<mytags:customer_ref>'.$row_feed['Invoice'].'</mytags:customer_ref>
<mytags:amount>'.$row_feed['Price'].'</mytags:amount>
<mytags:currency>GBP</mytags:currency>
</mytags:item>';
}
echo '</mytags:items>
</channel>
</rss>';
You have no xmlns (XML name space) defined. Thus the error is probably due to the fact that the root element items is... not defined, as said in the error message. See this link or that link for explanations on xmlns.
First, to complete the previous answer, after the first echo, add a rss-specific line: echo '<rss version="2.0">';
Second, you either need to add an xmlns definition in the above line echo '<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">'; for example, or to change the root element name. try with channel or feed. Better yet, find a feed which is read by the service you want your feed to be read by, and have a look at the beginning of the file; look for xmlns and root element names.
best,
My feeds are made with PHP and MYSQL into XML and then feedburner.
My problem is that dlvr.it cannot read new entries everytime I post.
Here's my PHP code:
<?php
include('db.php');
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
<channel>
<title>MindWeather Thesis</title>
<description>Latest News from my website</description>
<link>http://www.mindweather.info</link>';
$get_articles = "SELECT Fore_ID, Valid, Synopsis,
DATE_FORMAT(Issued,'%a, %e %b %Y %T') as formatted_date
FROM tblforecast ORDER BY Issued DESC LIMIT 15";
$articles = mysql_query($get_articles) or die(mysql_error());
while ($article = mysql_fetch_array($articles)){
echo '
<item>
<title>'.$article['Valid'].'-hour Forecast</title>
<description><![CDATA['.$article['Synopsis'].']]> </description>
<link>http://mindweather.info/fforecast.php?fore='.$article['Fore_ID'].'</link>
<pubDate>'.$article['formatted_date'].' GMT</pubDate>
</item>';
}
echo '</channel>
</rss>';
?>
The feedburner feed is located at: http://www.mindweather.info/feedz2.php
Your answers would be as of much help. Thanks!
The mechanism to detect new entries in a feed is to identify them using their unique <guid>. Since your feed lacks that element, it's perfectly normal that this service cannot detect new entries.
It's as simple has addding something like : <guid><?php echo $article['Fore_ID']; ?></guid>, assuming of course that the Fore_ID is unique. Add them between the <item> and </item>
You should also make sure each <link> element is unique because this is another way feed readers try to detect new/unique entries when they fail to do so using that <guid> element.
I am using the following php code to create an XML file:
echo '<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
<channel>
<title>mytitle</title>
<description>my description</description>
<link>www.mysite.com</link>';
$sql = "SELECT title, description, url, picture, formatted_date
FROM somewhere" ;
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
if(mysql_num_rows($result)>0)
{
while($result_array = mysql_fetch_assoc($result))
{
//timestamp date to xml format
$pubdate = date('D, d M Y H:i:s O', strtotime($result_array[formatted_date]));
echo '
<item>
<title>'.$result_array[title].'</title>
<description>'.$result_array[description].'</description>
<link>'.$result_array[indit_url].'</link>
<pubDate>'.$pubdate.' GMT</pubDate>
<enclosure url="'.$result_array[picture].'" length="1121990" type="image"/>
<image>
<url>'.$result_array[picture].'</url>
<title>image title</title>
<link>'.$result_array[picture].'</link>
<width>111</width>
<height>33</height>
<description>An amazing picture</description>
</image> </item>';
The file created is correctly validated using http://validator.w3.org/, but when i try to use some xml parser like: http://simplepie.org/ the perser are not able to capture the images. Am i using a correct tag to insert image into xml file? Is there any other tags or method to inser images?
Please try escaping the special characters in the image tag.
Use htmlspecialchars on $result_array[picture]
Also, can you show a sample of your rss feed here so that the answer can be more accurate.
I would suggest surrounding the data in an XML escape structure -as such:
<image>
<url><![CDATA[ imagedatagoeshere ]]></content></url>
</image>
However, I'm not familiar with simplepie, so I don't know if that understands CDATA properly.
I would like format the my RSS feed content. Like embed some information with Description tag. I am creating Wordpress Rss feed and trying to create rss 2.0
<?xml version="1.0"?>
<rss version="2.0">
<channel>
<item>
<title>firstquestion</title>
<url>test-domain.com</url>
<description>This is some ifnormation on the description. The below are the answers for the new question</description></item>
</channel>
</rss>
Now, I want to format or further some table or information to be attached with special characters, even html tags formatting in the <description> ... How can I do that?
When I simply insert , it gives me an error?
Use CDATA sections:
$description = '<strong>Strong formatting</strong> or <em>emphasis</em>.';
$item = '<item>
<title>firstquestion</title>
<url>test-domain.com</url>
<description><![CDATA['.$description.']]></description>
</item>';
You can have HTML inside the description element, but you have to encode it using htmlspecialchars.
$description = '<strong>Strong formatting</strong> or <em>emphasis</em>.';
$item = '<item>
<title>firstquestion</title>
<url>test-domain.com</url>
<description>'.htmlspecialchars($description).'</description>
</item>';