The Rss Feed help a website’s content to crawl and index fastly. For SEO prospects Rss Feed play important role for website. If you have a website in PHP based Framework, than this is right post for you to check How to Create Rss Feed.
Steps to Create Rss Feed Script Using PHP and MySQL:
1) First you have to understand the RSS feed XML file structure. Here is the xml format:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="ISO-8859-1"?> <rss version="2.0"> <post> <item> <title>Your Blog Title </title> <link>Complete link of yourblog</link> <description><![CDATA[ Short Description of your blog ]]></description> </item> </post> </rss> |
2) Now create a Posts table in your database using PhpMyadmin. Use below code to create a table and insert sample data in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
-- Table structure for table 'post' CREATE TABLE IF NOT EXISTS 'post' ( 'id' int(11) NOT NULL AUTO_INCREMENT, 'title' varchar(200) COLLATE utf8_unicode_ci NOT NULL, 'link' varchar(200) COLLATE utf8_unicode_ci NOT NULL, 'description' text COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY ('id') ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=7 ; -- Insert data for table `post` INSERT INTO 'post' ('id', 'title', 'link', 'description') VALUES (1, '5 Things You Should Never Search on Google', 'https://www.codefixup.com/5-things-you-should-never-search-on-google/', 'Keep safe your identity while searching on Google is very difficult today. If you regular use Google, yahoo or any other search engine for something than this post is very useful for you'), (2, 'How to Get the FEDEX Shipping Rates in PHP Via API Call', 'https://www.codefixup.com/how-to-get-the-fedex-shipping-rates-in-php-via-api-call/', 'To implement FEDEX shipping service on your website, use Fedex shipping rates api. In this post i will explain you how to send XML request to get FEDEX shipping rates'); |
3) Now you have to write PHP code to get the post data from database table and insert into an XML format. Make database connection first in php file and use looping to insert the data in XML format. Check below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php $db = new mysqli('localhost', 'your_user', 'your_pass', 'xmlfeed'); header("Content-Type: application/xml; charset=UTF-8"); // To output file in xml Format $rssfeed = '<?xml version="1.0" encoding="ISO-8859-1"?>'; $rssfeed .= '<rss version="2.0">'; $rssfeed .= '<post>'; $rssfeed .= '<title>Code Fixup Blog</title>'; $rssfeed .= '<link>http://www.codefixup.com</link>'; $rssfeed .= '<description>RSS feed of codefixup.com</description>'; $query = "SELECT * FROM 'post' ORDER BY sr DESC"; $result = $db->query($query) or die ("Could not execute query"); while($row = mysqli_fetch_array($result)) { extract($row); $rssfeed.= '<item>'; $rssfeed.= '<title>' . $title . '</title>'; $rssfeed.= '<description><![CDATA['. $desc. ']]></description>'; $rssfeed.= '<link>' . $link. '</link>'; $rssfeed.= '</item>'; } $rssfeed .= '</post>'; $rssfeed .= '</rss>'; echo $rssfeed; ?> |
Using mod rewrite you can change this feed file to .php to .rss or .rss. Just one more step left which allow visitors to check your RSS Feed on your website. To do this place following tag inspection of your website.
Thats all about for creating RSS Feed using PHP and MySql. Thanks for reading it.