Tuesday, 29 August 2017

Host Your Own RSS Feed Reader

rss feed
How to create an RSS feed from scratch


Today most of the systems we implement (such as WordPress) already come with a feed created and implemented, ready to validate, it is always possible to create a zero, or want to further customize what already exists.

Through a simple tutorial we will teach you to create an RSS feed from scratch, whether with static or dynamic content.

As always, first things first:

Reqirements for RSS feed:


A website with content, whether static or dynamic.

A Web editor.

Process of creating RSS feed:


To create an RSS feed, it is necessary to encode a file in XML format, which contains the necessary tags to interpret and display the content in the specified format, to which users will access. This file we can leave it native in XML, or embed it inside PHP, as we will see in both cases below.

For this we will perform 2 processes. In the first instance we will create a static feed, from content that we have available on our site.

After that we will repeat the task, but this time we will retrieve contents from a database to put them into the feed, ideal if they are publishing through a CMS or if they obtain content directly from a database.

So without further ado, let's get down to business:

1.- Creating a static RSS feed


This option is valid only if we have static content on our site, which although not very justified, can be a useful functionality to deliver to visitors.

For this, we need to create an XML file in our Web editor, and start coding:

The first 2 lines that we added correspond to the definition of the version and coding of xml (in order for it to validate correctly we must add the version, as far as the coding is optional but it never is) and the version of RSS that will use The file we are creating. Currently most sites offer subscriptions for both RSS 1.1 and 2.0, but with the latter is more than enough. At the end we close the rss tag to finish the document.

Following the above, it is necessary that everything related to the feed (both information and content), be included within the </ channel> tags, which we will add just after the previous code.

And now, inside the channel tags we will be adding the feed information.

And finally, after the information, we add the related content that will be displayed in the feed, we will do this by repeating the same tags we used previously, but closed between < item>and </ item>:

The above we save it with any name and with extension xml, we upload it and when opening it in the navigator we will be able to see that our static feed is completely ready:

2.- Creating a dynamic RSS feed


We created our static feed, its just onnly a few minutes, we realized that it was very simple and we are ready to conquer the world ... right ?. Well, not yet. As we indicated in the previous point, a static feed is not very funny, since we must add each new content manually and ends up being a lot of work, or it may be that on more than one occasion we forget to update. Due to this, if we have a site where the content is obtained dynamically from a database, it is possible to adapt that to our feed and that the new information will be added as it is generated. For this, we will work with both XML and PHP, in a bombastic that will spark and make us the most popular kids in high school.

For this, we will consider that we have a database, which has a news table with a title field, a link field, a detail field and a date field of the news, as we see below:

[Table id = 1 /]

So, the first thing to do is create a PHP file in our Web editor.

As you can see, in the previous code we left a blank section, between the <? Php and?> Tags. This part of the code is where we will connect to the database, rescue the news and deploy it dynamically.

For this, we will first connect to the database. As a good practice we will use DEFINE to define redundancy, define the connection data, but if you want, you can skip this part and execute the function directly:

And now we make the connection. We run the query.

Now that we have retrieved all the news from the database in an orderly way by date, obtaining the newest ones first, we will pass them to an array to write them in the specified XML format, as follows:

With the above, we execute a cycle that will pass to an array in an associative way (thanks to the parameter MYSQL_ASSOC, which will allow us to access the records in the array with the same name that they have in the table of the database). That they are finished and will create a new item with the respective values to include in the feed.

With that we are almost ready, the last detail is to add a small header at the beginning of the document, indicating that we are executing XML content for better recognition at the time of executing the complete script, we do this by adding at the beginning.

And ready!.

We have our dynamic RSS feed, which will add the results obtained from the database and each time we publish new content will be updated automatically.

And now, to conquer the world ?. Not yet. Before finishing:

3.- Useful tips for launching RSS feed


Some tips that are never over:

Validate feed

Before launching our feed to the public, it's good to make sure that it works properly and that we have not made coding errors. For this we can validate through several services, such as:

W3C Validator

Feed Validator

Just add the URL of our feed and validate.

Publish the feed

There are services that allow us to publish our feed and offer a series of interesting features, such as adding advertising or statistics about our subscribers. The best known and undoubtedly used is Feedburner, which in only moments we can publish our feed without problems.

Show users that a RSS feed exists on the site

If at this moment they look at the address bar of your browser, you will see that there is an orange icon.
This icon indicates that the site has an RSS feed to which they can subscribe. How to add it? Very simple, we should only add the following lines between the <head> and </ head> tags of each page we want it to appear in, such as a favicon. Simple, right?

and </ head> tags of each page we want it to appear in, such as a favicon. Simple, right?

Customize your RSS feed


The details certainly make the difference in the minds of the users. So, now that we've learned how to make a feed from scratch, why not play it a bit and make it more personalized? Only by modifying small parts of the code can we display certain types of information in our feed (for example comments if they exist), or limit the number of articles to be displayed, or make a separate feed for each person that writes in the site and thus each Reader follow his favorite author.

The possibilities are many, you just have to experiment.

And with this we have finished this tutorial in which we have covered the process of creating an RSS feed from scratch. As you can see, it only takes a few minutes and the difficulty is low, so we encourage you to try and tell us your experience.

Before ending, we remind you that as always, this tutorial has been:

Any questions or comments you may have, we invite you to leave us a few lines in the area enabled below.
We hope this tutorial has been useful for you.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.