How to Create an RSS Feed with PHP and MySQL

This example displays a web page with single button, when we click on this button, an XML file will be displayed in the RSS format. But the content of this file will be loaded dynamically from the database.

1. RSS sample

This is an example for the rss output in xml format.
  1. <rss version="2.0">
  2. <channel>
  3. <title>Bewebdeveloper : Free web tutorials</title>
  4. <link>link here</link>
  5. <description>description</description>
  6. <item>
  7. <title>Title-1</title>
  8. <link>Link-1</link>
  9. <description>Desc-1</description>
  10. </item>
  11. <item>
  12. <title>Title-2</title>
  13. <link>Link-2</link>
  14. <description>Desc-2</description>
  15. </item>
  16. </channel>
  17. </rss>

1. Database

Create a new database named "rss_feed", and import the SQL script in main folder, or copy/paste the script bellow:
  1. --
  2. -- Table structure for table `post`
  3. --
  4.  
  5. CREATE TABLE IF NOT EXISTS `post` (
  6. `id` int(11) NOT NULL AUTO_INCREMENT,
  7. `title` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
  8. `link` varchar(200) COLLATE utf8_unicode_ci NOT NULL,
  9. `description` text COLLATE utf8_unicode_ci NOT NULL,
  10. PRIMARY KEY (`id`)
  11. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=7 ;
  12.  

3. Files

index.php

This is the main file, it will be displayed on the first view
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>How to Create an RSS Feed with PHP and MySQL</title>
  6. <link rel="stylesheet" href="css/style.css" />
  7. </head>
  8.  
  9. <body>
  10. <div class="container">
  11. <h1 class="main_title">How to Create an RSS Feed with PHP and MySQL</h1>
  12. <div class="content">
  13. <a href="rss.php" class="button_rss">Click to get RSS feed</a>
  14. </div><!-- content -->
  15. </div><!-- container -->
  16. </body>
  17. </html>

rss.php

The rss feed will be generated here. W’ll load the list of posts from the database using PHP PDO, and display this content as an xml file using the php header, header('Content-Type: application/xml');
  1. <?php
  2. // PDO connect *********
  3. function connect() {
  4. return new PDO('mysql:host=localhost;dbname=rss_feed', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
  5. }
  6.  
  7. $pdo = connect();
  8.  
  9. // posts *******************************
  10. $sql = 'SELECT * FROM post ORDER BY id DESC';
  11. $query = $pdo->prepare($sql);
  12. $query->execute();
  13. $rs_post = $query->fetchAll();
  14.  
  15. // The XML structure
  16. $data = '<?xml version="1.0" encoding="UTF-8" ?>';
  17. $data .= '<rss version="2.0">';
  18. $data .= '<channel>';
  19. $data .= '<title>Title</title>';
  20. $data .= '<link>Link</link>';
  21. $data .= '<description>Description</description>';
  22. foreach ($rs_post as $row) {
  23. $data .= '<item>';
  24. $data .= '<title>'.$row['title'].'</title>';
  25. $data .= '<link>'.$row['link'].'</link>';
  26. $data .= '<description>'.$row['description'].'</description>';
  27. $data .= '</item>';
  28. }
  29. $data .= '</channel>';
  30. $data .= '</rss> ';
  31.  
  32. header('Content-Type: application/xml');
  33. echo $data;
  34. ?>

style.css

This is the CSS file, it contain the design styles (optional).
  1. * {
  2. margin: 0;
  3. padding: 0;
  4. }
  5. body {
  6. padding: 10px;
  7. background: #eaeaea;
  8. text-align: center;
  9. font-family: arial;
  10. font-size: 12px;
  11. color: #333333;
  12. }
  13. .container {
  14. width: 1000px;
  15. height: auto;
  16. background: #ffffff;
  17. border: 1px solid #cccccc;
  18. border-radius: 10px;
  19. margin: auto;
  20. text-align: left;
  21. }
  22. .header {
  23. padding: 10px;
  24. }
  25. .main_title {
  26. background: #cccccc;
  27. color: #ffffff;
  28. padding: 10px;
  29. font-size: 20px;
  30. line-height: 20px;
  31. }
  32. .content {
  33. padding: 10px;
  34. min-height: 100px;
  35. text-align: center;
  36. }
  37. .footer {
  38. padding: 10px;
  39. text-align: right;
  40. }
  41. .footer a {
  42. color: #999999;
  43. text-decoration: none;
  44. }
  45. .footer a:hover {
  46. text-decoration: underline;
  47. }
  48. .button_rss {
  49. width: 200px;
  50. height: 30px;
  51. display: block;
  52. text-align: center;
  53. background: #a3a3a3;
  54. border: 1px solid #808080;
  55. color: #ffffff;
  56. border-radius: 10px;
  57. cursor: pointer;
  58. margin: auto;
  59. margin-top: 20px;
  60. font-size: 14px;
  61. line-height: 30px;
  62. font-weight: bold;
  63. text-decoration: none;
  64. }

source : http://www.bewebdeveloper.com/tutorial-about-how-to-create-an-rss-feed-with-php-and-mysql

Post a Comment