Este sitio web utiliza cookies para realizar análisis y mediciones de tus visitas. [ Acepto ] [ Más información aquí ].

How to Create a RSS Feed for a Blog

With this tutorial, you will learn how to create a RSS feed for your blog. To see an example of a RSS feed, visit https://www.danielpinero.com/feed. To create your own feed, write a PHP file following these steps:

1. Create an array with all posts

Make a query to your blog's MySQL database, and add title, metadescription and URL of each post to a string ($feed), in this way:

$result = mysql_query("SELECT `title`, `description`, `url` FROM `blog`

ORDER BY `date` DESC;");

while($row = mysql_fetch_array($result)){

$feed.= "<item><title>".$row['title']."</title>

<description>".$row['description']."</description>

<link>http://www.tublog.com/".$row['url']."/</link>

</item>";

}

2. Nest the $feed string

Next, nest the string $feed under the general description of your blog:

$feed = "<?xml version='1.0'?>

<rss version='2.0'>

<channel>

<title>Your blog's title</title>

<description>Your blog's description</description>

<link>http://yourblogaddress.com</link>" . $feed . "</channel></rss>";

3. Create the RSS file

This last line will definitely create the RSS file as "feed" (with no extension).

file_put_contents("feed", $feed);

Save the PHP file and run it. Your first RSS feed will be created with all the posts from your blog. As well, you can run this PHP file through a cron job so it runs automatically in a predetermined frequency.

4. Validate the RSS feed

It's very important that you validate the RSS feed so that applications like Feedly can effectively recognize it. For this, use the W3C Feed Validation Service.

Añadir comentario