How To Load Rss Into Php?

RSS and php

  • Hello... I'm new to php and XML, and am trying to create an RSS feed using php/MySQL. It's strange... if I add this to MyYahoo as an RSS feed it shows up. But if I run it through a validator, it doesn't validate (an extra line is added to the top of the file). And when I look at it through IE, I see what looks like a valid RSS feed about 1/3 of the time, the rest it gives errors. What I would like is someone to figure out what's wrong with the script I've written, fix it (so that it validates), and give me a description of what they did (so I can fix it myself the next time). The script is based off of this one: http://www.xmlmania.com/documents_article_34.php The validator I'm using is: http://feedvalidator.org/ Thanks!

  • Answer:

    Hi, The biggest problem with the demo script is that the dates are not formatted correctly. I've changed that, and my validation is fine. However, what I noticed within their ( the site you got this script from) news feed: http://www.xmlmania.com/xml/feeds/news.php is they have HTML formatting inside the DESCRIPTION tags, which will not validate. Most of the time the browsers and RSS Readers know what to do with HTML coding inside the DESCRIPTION tag, but the validators do not, because it is not valid RSS. Some other notes just to help out, is the SQL table should not have the name of a MySQL keyword as a field name, such as TEXT or DATE. Something like this would be more appropriate. create table rssnews( id int(10) not null auto_increment primary key, title varchar(200), newstext text, newsdate datetime ); And the revised PHP code is here: ---BEGIN CODE --- <?php // change the sql look up so that you can format the date correctly// // header ("Content-type: text/xml"); echo ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); ?> <rdf:RDF xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:hr="http://www.w3.org/2000/08/w3c-synd/#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/"> <channel rdf:about="http://www.scarn.com/rss/news.php"> <title>News Feed</title> <description>News Feed Test for RSS</description> <link>http://www.scarn.com/rss/news.php</link> </channel> <?php mysql_connect ("localhost", "testman", "testword") or die ("Cannot connect to database server."); mysql_select_db ("test") or die ("Cannot connect to database."); // change the sql look up so that you can format the date correctly// // $result = mysql_query ("select * from rssnews order by id desc limit 0,10") or die (mysql_error()); $myq = 'SELECT id, title, newstext, DATE_FORMAT( newsdate, \'%Y-%m-%d\' ) AS mydate' . ' FROM rssnews' . ' ORDER BY id DESC ' . ' LIMIT 0 , 10'; $result = mysql_query($myq) or die (mysql_error()); // change the item to items as directed by the RSS Validator// while ($row = mysql_fetch_array ($result)) { echo (" <item rdf:about=\"http://www.scarn.com\"> <title>"); echo $row['title']; echo ("</title> <description>"); echo $row['newstext']; echo ("</description> <link>http://www.scarn.com/news_article_"); echo $row['id']; echo (".php"); echo ("</link> <dc:date>"); // USE NEW DATE FORMAT // echo $row['mydate']; echo ("</dc:date> </item>\n\n"); } mysql_free_result ($result); ?></rdf:RDF> --- END CODE --- Some Resources W3 Date Formats Different standards may need different levels of granularity in the date and time, so this profile defines six levels. Standards that reference this profile should specify one or more of these granularities. If a given standard allows more than one granularity, it should specify the meaning of the dates and times with reduced precision, for example, the result of comparing two dates with different precisions. http://www.w3.org/TR/NOTE-datetime DATE_FORMAT(date,format) Formats the date value according to the format string. The following specifiers may be used in the format string: http://dev.mysql.com/doc/mysql/en/Date_and_time_functions.html Some thoughts: The whole article shouldn't be in a RSS Feed anyway, so not having the HTML formatting in there shouldn't be a problem, but, if you feel it is, and you want to be spared the task of having two separate areas (website text data and the RSS Feed text data), you can use something like what they are showing on this website to change the outgoing text http://www.php.net/manual/en/function.ob-start.php Down in the example it shows how to use the call back function to alter text using str_replace. Personally, I would not put the HTML in there at all, and have a separate table for the RSS feed, so that I could have HTML formatting on my page. It is a bit more work, but this way it will always validate, and any RSS reader will be able to see it without problems. If you have any other questions regarding this please use the Clarification Request button. I will be happy to help you out with anything else on this script that doesn't seem to be working properly. thanks, webadept-ga

muonwar-ga at Google Answers Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.