Create RSS with PHP and deliver it with FeedBurner
1087   2  

Create RSS with PHP and deliver it with FeedBurner

  31 Oct 2017
  Coding Category
Create RSS with PHP and deliver it with FeedBurner

  25% more discounted by using the Giliapps as code in HostGator

In this article we learn to write simple code with Procedural PHP and MySQL to create a rss.xml file and then automate this creation and finally add it to Google FeedBurner to deliver latest Blog posts to subscribers.

Suppose that we have a table in MySQL that contains our Blog posts. Also we have a registered account on Google FeedBurner. Lets start:

1. Create a folder with name feed in root.

2. Create a file with name index.php, and also a file with name rss.xml in feed folder.

3. Place your code to connect to MySQL DB. Normally these codes appear in config.php in root and use every where needed. So create that and also edit DB properties:

<?php

define( "DB_SERVER", "localhost" );

define( "DB_DATABASE", "dbname" );

define( "DB_USERNAME", "username" );

define( "DB_PASSWORD", "password" );

$db = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

if(!$db ) {

die('MySQL Connection Error: ' . mysql_error());

} else {

mysqli_set_charset($db, "utf8"); //I use it here just for beauty :-) You can use it outside if, and it should be used for supporting non-latin chars

}

//define a function to convert some chars in rss

function rssxml($str) {

$str = str_replace('<', '&lt;', $str);

$str = str_replace('>', '&gt;', $str);

$str = str_replace('"', '&quot;', $str);

$str = str_replace("'", '&#39;', $str);

$str = str_replace("&", '&amp;', $str);

return $str;

}

?>

4. Now insert main code to create XML file (rss.xml) with our posts in a MySQL Table (edit your info and also MySQL Table properties):

<?php

header("Content-Type: application/rss+xml; charset=UTF-8"); //need to create correct encoding rss file

include_once("../config.php"); //call configuration if needed

$rssfeed = '<?xml version="1.0" encoding="utf-8"?>';

$rssfeed .= '<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">'; //I use two xmlns to connect to Apple News and also using dc:creator

$rssfeed .= '<channel>';

$rssfeed .= '<title>Blog Title</title>';

$rssfeed .= '<link>https://www.yourdomain.com/blog/</link>';

$rssfeed .= '<lastBuildDate>' . date("Y-m-d h:i:sa") . '</lastBuildDate>'; //creation time

$rssfeed .= '<description>Your Blog Description</description>';

$rssfeed .= '<language>en-US</language>'; //use language code with country code

$rssfeed .= '<generator>Brand Name (https://www.yourdomain.com)</generator>';

$rssfeed .= '<copyright>Copyright © ' . date("Y") . ' Brand Name</copyright>';

//now start query for list your latest post, normally 10 or more, someone said 500!

$query = "SELECT * FROM tablename ORDER BY id DESC LIMIT 10";

if($result = mysqli_query($db, $query)) {

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

$rssfeed .= '<item>';

$rssfeed .= '<title>' . rssxml($row['posttitle']) . '</title>'; //use defined function to ensure that text contains true chars

$rssfeed .= '<category>' . $row['category'] . '</category>';

$rssfeed .= '<description>' . rssxml($row['posttext']) . '</description>'; //if you want to insert post image, you can use '<description><![CDATA[<img src="' . $row['postimageurl'] . '" alt="' . $row['postimagealttext'] . '"> ' . $row['posttext'] . ']]></description>';

$rssfeed .= '<link>https://www.yourdomain.com/blog/' . $row['posturl'] . '</link>'; //if you use url parameters, just add it here, no problem

$rssfeed .= '<pubDate>' . $row['postdate'] . '</pubDate>';

$rssfeed .= '<dc:creator>' . $row['postcreator'] . '</dc:creator>';

$rssfeed .= '</item>';

}

}

$rssfeed .= '</channel>';

$rssfeed .= '</rss>';

file_put_contents("rss.xml", $rssfeed); //put generated text in rss.xml

header("refresh:0;url=rss.xml"); //just for some users that link to this folder without point rss.xml

?>

5. Now you can manually create rss.xml by visiting https://www.yourdomain.com/feed/ or automate this by Cron Jobs like this at every day or week or ... in HostGator:

/opt/php56/bin/php /home/yourdoma/public_html/feed/index.php

6. Finally go to FeedBurner and set these:

⦿ link to your feed like https://www.yourdomain.com/feed/rss.xml

⦿ Name your FeedBurner Feed like BlogTitle

⦿ After connecting go to Publicize tab and click Email Subscriptions

⦿ Active this option and copy first generated code to you Website

⦿ You can customize time of sending E-mails and many other useful options

⦿ I recommend these options for activation: PingShot (in that tab) and BrwserFriendly and SmartFeed (in Optimize tab)

7. Leave a comment if have question, specially I will be happy to improve these article by correction codes. This article presented in English only. Other languages come later!

Author: Ismael Azaran

Do you like it? Share it with your friends.

Categories

Tags