Creating a system for your RSS news with PHP 4 and 5 PDF Print E-mail
User Rating: / 0
PoorBest 
Thursday, 15 May 2008

I. Introduction
Firstly, what is RSS?
It is a syndication format (diffusion in a way), this will help disseminate information (usually news, articles and even new posts in forums) and allow other people to view this information without your visit site. This information will be disseminated in the form of XML. You can view this information with a feed reader. Some browsers (IE 7, Firefox, Opera, etc.) have a feed reader, as well as some search engine (Google, Yahoo) and soon operating system (Windows Vista). But there are also independent readers.

Specifically, an RSS feed is nothing more than a simple XML file. The only thing we will therefore do is to maintain this RSS feed and tell the browser where is this flow.
II. XML file structure
To begin with, it will of course we have to create an XML file, and we must of course comply with a certain structure for this file. So how come such a file:
XML file structure
<? xml version = "1.0" encoding = "ISO-8859-15"?>
<rss version="2.0">
         <channel id="xxx">
                 <description> description Channel </ description>
                 <link> A link to the page </ link>
                 The title of <title> channel </ title>
         </ channel>
</ rss>

As you see, you have a tag RSS, the root tag of the file, which contains a tag channel (you can have other channels of course). A channel is a kind of category, therefore nothing to stop you if you wish, to a category (channel) I put you in which each important message to the forum. Each tag contains a tag channel description, a link tag and a tag title which, as these names leave guess, educate the description, link and the title of the channel in question. I just added an id to channel then to simplify the search for this tag. You can also put other tags inside of this tag channel, but we will content ourselves with them. Here is a complete list of tags may be included in channel:
Tags possible for the node channel
<title> title = Channel
<link> = link to the page in question
<description> = description Channel
<language> = The language channel
<copyright> = The copyright of the channel
<managingEditor> = Email address of the person responsible
<webMaster> = Email address of the webmaster of the site
<pubDate> = First publication of the channel
<lastBuildDate> = Last publication of channel
<category> = category Channel
<generator> = Programme used to generate the flow
<docs> = Documentation format RSS employee
<ttl> = time before the next refresh
<image> = Image displayed with the channel
The note <rating> = channel of PICS
<textInput> = To add a text input area
<skipHours> = hours than feed readers (aggregators) can ignore
<skipDays> = The day that feed readers (aggregators) can ignore
Now it is all very well, we have a channel, but we must put something dedans.Pour this, we must also respect a certain structure:
Structure of an item
<item>
   <title> title element </ title>
   <link> link the item </ link>
   <pubDate> date of publication of the item </ pubDate>
   <description> Item Description </ description>
   <author> News author </ author>
</ item>

Here again, this is not too complicated to understand. For UN element, it was a tag item, in which you have a tag title, link, pubDate and description which, as their names indicate, inform the title, link to the item, the date of publication and description of the item. Again, we can save other tags including the following list:
Tags possible for the node item
<title> = The title of element
<link> = link the item
<description> = Item Description
<author> = Mail the author of element
<category> = category this element
<comments> = link to a page of comments about the item
<enclosure> = Media Attached to this object
<guid> = text which uniquely identifies this item (on the title here is usual)
<pubDate> = Publication
<source> = Channel which the item is
You have therefore understood that each new news on your site, you will have to add an object item in the XML file. This is what we will see in later chapters.

III. Add the news in the XML
To add news in the XML file, you have two solutions:
You add news every hand in the XML file. This solution is valid for a small number of news to enter the stream.
You create a method that, whenever you add a news site, adds the news in the XML file. This method is more complex to implement, but then much more convenient, since nothing remains to be done. It is the one that we'll see.
To manipulate XML files with PHP, you can use either DOM XML if a PHP 4 or DOM if a PHP 5. Since both version can be used, I will whenever the two versions of each code.

Specifically, here is what we will do:

Methods to create
One method that will create an XML file containing only empty channel
One method that will open the XML file exists
One method that will save the XML file
One method that will add a news: for use with each new news
If your site does not yet have news, you can move from the second method.

Assuming your table now that news is composed as follows:
Structure of our table News
CREATE TABLE `news` (
   `ID` int (11) NOT NULL auto_increment,
   `title` varchar (100) NOT NULL default''
   `` timestamp int (12) NOT NULL default'0 ',
   `` contents text NOT NULL,
   `author` varchar (100) NOT NULL default''
   PRIMARY KEY `ID` ( `ID`)
);

You can of course have a completely different structure, then you will only change the code accordingly.
A-III. Method Opening
We will now create the method will open the XML file. What does it do? It opens the XML file and returns a reference to the roster.
Method with PHP4
openXML function () (

/ / Open File
$ File = domxml_open_file ( "CheminDuFichier.xml");

/ / It returns the XML file
Return $ file;
)

Method with PHP5
openXML function () (

/ / Open File
$ File = new DOMDocument ();
$ File-> load ( "CheminDuFichier.xml");

/ / It returns the file
Return $ file;
)

You see that the syntax is fairly simple to use XML and is not that different between PHP4 and PHP5. The true change is to be DOM object-oriented.

III-B. Method for creating file
We will now write a method that will initialize the XML file. It will add the channel in the file and data channel, then return a reference to the roster.
Method with PHP4
createXML function () (

/ / Create file into memory
$ File = domxml_new_doc ( "1.0");

/ / Creation of root knot
$ root = $ file-> create_element ( "rss") / / It creates the root element
$ root-> set_attribute ( "version", "2.0") / / It adds the attribute version (2.0)
$ root = $ file-> append_child ($ root); / / inserts root in the document

/ / Create node channel
element_channel $ = $ file-> create_element ( "channel ");// It creates a channel
$ element_channel-> set_attribute ( "id", "news") / / It gives an id attribute to our channel
element_channel $ = $ root-> append_child ($ element_channel) / / It adds that element to the root

/ / Create node description
element_description $ = $ file-> create_element ( "description ");// It creates an element description
element_description $ = $ element_channel-> append_child ($ element_description) / / It adds that element to channel

/ / Create text for the node description
texte_description $ = $ file-> create_text_node ( "Description of the channel") / / It creates a text
texte_description $ = $ element_description-> append_child ($ texte_description) / / We insert this text in the description node

/ / Creation of node link and adding text to the element
Element_link $ = $ file-> create_element ( "link");
element_link $ = $ element_channel-> append_child ($ element_link);
texte_link $ = $ file-> create_text_node ( "Link to the site");
texte_link $ = $ element_link-> append_child ($ texte_link);

/ / Create node title and added to the text element
Element_title $ = $ file-> create_element ( "title");
element_title $ = $ element_channel-> append_child ($ element_title);
texte_title $ = $ file-> create_text_node ( "Title Channel");
texte_title $ = $ element_title-> append_child ($ texte_title);

/ / It returns the XML file
Return $ file;
)

Method with PHP5
createXML function () (

/ / Create file into memory
$ File = new DOMDocument ( "1.0");

/ / Creation of root knot
$ root = $ file-> createElement ( "rss") / / It creates the root element
$ root-> setAttribute ( "version", "2.0") / / It adds the attribute version (2.0)
$ root = $ file-> appendChild ($ root); / / inserts root in the document

/ / Create node channel
element_channel $-$ file => createElement ( "channel ");// It creates a channel
$ element_channel-> setAttribute ( "id", "news") / / It gives an id attribute to our channel
element_channel $ = $ root-> appendChild ($ element_channel) / / It adds that element to the root

/ / Create node description
element_description $ = $ file-> createElement ( "description ");// It creates an element description
element_description $ = $ element_channel-> appendChild ($ element_description) / / It adds that element to channel

/ / Create text for the node description
texte_description $ = $ file-> createTextNode ( "Description of the channel") / / It creates a text
texte_description $ = $ element_description-> appendChild ($ texte_description) / / We insert this text in the description node

/ / Creation of node link and adding text to the element
Element_link $ = $ file-> createElement ( "link");
element_link $ = $ element_channel-> appendChild ($ element_link);
texte_link $ = $ file-> createTextNode ( "Link to the site");
texte_link $ = $ element_link-> appendChild ($ texte_link);

/ / Create node title and added to the text element
Element_title $ = $ file-> createElement ( "title");
element_title $ = $ element_channel-> appendChild ($ element_title);
texte_title $ = $ file-> createTextNode ( "Title Channel");
texte_title $ = $ element_title-> appendChild ($ texte_title);

/ / It returns the XML file
Return $ file;
)

I will not comment on anything new code, since the comments in the code should be sufficient to understand it.
As you can see the establishment is always in a hierarchical manner: it creates an element, it adds to the root, we will create a new element, it adds to the parent element, and so forth. It always happens and the manipulation of XML files.

III-C. Method of adding a news
We will now create the method that will allow us to add some news in our XML file.
Method with PHP4
addOneNews function ($ file, $ title, $ timestamp, $ author) (

/ / It gets the channel
element_channel $ = $ file-> get_element_by_id ( "news");

/ / Create node item
Element_item $ = $ file-> create_element ( "item");
element_item $ = $ element_channel-> appendChild ($ element_item);

/ / Create node title and added to the text element
Element_title $ = $ file-> create_element ( "title");
element_title $ = $ element_item-> append_child ($ element_title);
Texte_title $ = $ file-> create_text_node ($ title);
texte_title $ = $ element_title-> append_child ($ texte_title);

/ / Creation of node link and adding text to the element
Element_link $ = $ file-> create_element ( "link");
element_link $ = $ element_item-> append_child ($ element_link);
texte_link $ = $ file-> create_text_node ( "Link to the news");
texte_link $ = $ element_link-> append_child ($ texte_link);

/ / Create node pubDate and adding text to the element
element_date $ = $ file-> create_element ( "pubDate");
element_date $ = $ element_item-> append_child ($ element_date);
texte_date $ = $ file-> create_text_node ($ date ( "d / m / YH: i", $ timestamp));
texte_date $ = $ element_date-> append_child ($ texte_date);

/ / Create node author and adding text to the element
element_author $ = $ file-> create_element ( "author");
element_author $ = $ element_item-> append_child ($ element_author);
texte_author $ = $ file-> create_text_node ($ author);
texte_author $ = $ element_author-> append_child ($ texte_author);
)

Method with PHP5
addOneNews function ($ file, $ title, $ timestamp, $ author) (

/ / It gets the channel
element_channel $ = $ file-> getElementById ( "news");

/ / Create node item
Element_item $ = $ file-> createElement ( "item");
element_item $ = $ element_channel-> appendChild ($ element_item);

/ / Create node title and added to the text element
Element_title $ = $ file-> createElement ( "title");
element_title $ = $ element_item-> appendChild ($ element_title);
Texte_title $ = $ file-> createTextNode ($ title);
texte_title $ = $ element_title-> appendChild ($ texte_title);

/ / Creation of node link and adding text to the element
Element_link $ = $ file-> createElement ( "link");
element_link $ = $ element_item-> appendChild ($ element_link);
texte_link $ = $ file-> createTextNode ( "Link to the news");
texte_link $ = $ element_link-> appendChild ($ texte_link);

/ / Create node pubDate and adding text to the element
Element_date $ = $ file-> createElement ( "pubDate");
element_date $ = $ element_item-> appendChild ($ element_date);
texte_date $ = $ file-> createTextNode ($ date ( "d / m / YH: i", $ timestamp));
texte_date $ = $ element_date-> appendChild ($ texte_date);

/ / Create node author and adding text to the element
element_author $ = $ file-> createElement ( "author");
element_author $ = $ element_item-> appendChild ($ element_author);
Texte_author $ = $ file-> createTextNode ($ author);
texte_author $ = $ element_author-> appendChild ($ texte_author);
)

There is nothing new in this part of code, since we have already dealt with the highest hierarchical organization.

III-D. Méthode backup
It will now move into the backup method. It does this method make it more convenient for our code, thereby passing PHP4 to PHP5 or vice versa, all the functions DOM and DOMXML occur in the php file which does qu'appeler methods, it is enough to change a file RSS.php by the other and voila.
Method PHP4
saveXML function ($ file) (

/ / Save File
$ File-> dump_file ( "CheminDuFichier.xml");
)

Method PHP5
saveXML function (file $) (

/ / Save File
$ File-> save ( "CheminDuFichier.xml");
)

III-E. Initialization XML
Now the code that will allow you to initialize your XML file:
Initialization XML
require ( "RSSPHP.php");

/ / Create XML file
$ file = createXML ();

mysql_connect ( "localhost", "pseudo", "mdp") or die (mysql_error ());
mysql_select_db ( "db") or die (mysql_error ());

$ query_news = mysql_query ( "SELECT * FROM news") or die (mysql_error ());

while ($ data_news = mysql_fetch_array ($ query_news)) (
addOneNews ($ file, $ data_news [ 'title'], $ data_news [ 'timestamp'], $ data_news [ 'author']);
)

mysql_close ();

saveXML ($ file);

This code is only create the XML file and call the method addOneNews for each news that is in the database. You will normally use only once this code, the first time that XML is ready to welcome new News.

III-F. Example: Adding a news
Now that we have our beautiful code that works, we must use it, unless you've done to make nice ...
As our example works with the news, I'll be making the code inserted in a news database. Let's say this page is drawn from a form, it would give us a code like this:
Use concrete
extract ($ _POST);

require ( "RSSPHP.php");

mysql_connect ( "localhost", "username", "mdp") or die (mysql_error ());
mysql_select_db ( "db") or die (mysql_error ());

/ / Add the news in the database
mysql_query ( "INSERT INTO news (title, timestamp, content, author) VALUES
( '. "$ Title. "','". $ Timestamp. "','". $ Contents. "','". $ Author. "')") Or die (mysql_error ());

/ / Open File
$ file = openXML ();

/ / Add the news in the file
addOneNews ($ file, $ title, $ timestamp, $ author);

saveXML ($ file);

mysql_close ();

You can see that for someone who has already made its script news, the change is minimal. Just call the method addNews and voila.
If this is the first time you add the RSS, we must not forget that before using the code I've provided in the previous chapter for the xml is already created and initialized channel before you enter items inside.

III-G. Please inform the browser that you have an RSS feed
Now that all your news are placed directly in XML and your XML file is complete, it is important that you give to your browser and feed readers (aggregators) whereabouts of this file. For this, you just add the following line in the head tag on your page:
Tag to add to indicate that there is an RSS feed on your site
<link rel="alternate" type="application/rss+xml" title="Flux RSS" href="Lien to file XML" />

Normally, if everything went well and that you browse your site with Firefox, you should see this logo displayed next to the URL of your page. You can now read the RSS feeds from your site with any feed reader.

IV. Conclusion
A-IV. Conclusion
In this example, we created an RSS feed for news site, but one can also imagine putting something else, such as posts of a forum, the comments of a blog and the list goes on. The way to do would be exactly the same, you just need to adapt the data you want to bring in this new feed.

IV-B. Other formats Syndication
Although we have treated as RSS, there are other formats Syndication, whose best known (not counting RSS) is Atom. It is very similar to RSS, but aims to be more flexible, it can be distributed via the HTTP protocol. Its strong point is especially its approach more professional than RSS.

The syntax of XML ATOM is more complicated than RSS. Here is an example:

<? xml version = "1.0" encoding = "utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
   <title type="text"> dive into mark </ title>
   <subtitle type="html">
     A lot <em> </ em> of effort
     went into making this effortless
   </ subtitle>
   <updated> 2005-07-31T12: 29:29 Z </ updated>
   <id> tag: example.org, 2003:3 </ id>
   <link rel = "alternate" type = "text / html"
    hreflang = "" href = "http://example.org/" />
   <link rel = "self" type = "application / atom + xml"
    href = "http://example.org/feed.atom" />
   <rights> Copyright (c) 2003, Mark Pilgrim </ rights>
   <generator uri="http://www.example.com/" version="1.0">
     Example Toolkit
   </ generator>
   <entry>
     <title> Atom draft-07 snapshot </ title>
     <link rel = "alternate" type = "text / html"
      href = "http://example.org/2005/04/02/atom" />
     <link rel = "enclosure" type = "audio / mpeg" length = "1337"
      href = "http://example.org/audio/ph34r_my_podcast.mp3" />
     <id> tag: example.org, 2003:3.2397 </ id>
     <updated> 2005-07-31T12: 29:29 Z </ updated>
     <published> 2003-12-13T08 :29:29-04: 00 </ published>
     <author>
       <name> Mark Pilgrim </ name>
       <uri> http://example.org/ </ uri>
       <email> This e-mail address is being protected from spam bots, you need JavaScript enabled to view it </ email>
     </ author>
     <contributor>
       <name> Saturday Ruby </ name>
     </ contributor>
     <contributor>
       <name> Joe Gregorio </ name>
     </ contributor>
     <content type = "xhtml" xml: lang = ""
      xml: base = "http://diveintomark.org/">
       <div xmlns="http://www.w3.org/1999/xhtml">
         <p> <i> [Update: The Atom draft is finished.] </ i> </ p>
       </ div>
     </ content>
   </ entry>
</ feed>

If you want more info on this format, you will find an introduction to this format in the links at the bottom this page, which should help you take control of Atom.

 
< Prev   Next >
School Joomla Templates and Joomla Tutorials