Build a Dynamic RSS Feed With PHP and MySQL

By | 24 June 2017

NOTE: Wherever you see [url] please insert your web sites url instead of [url].

In every single place I look I can discover tutorials however not often are they full. The tutorial I’m about to write down is supposed to be an entire tutorial. Positive you’ll be able to add extra choices to the RSS file itself however what I imply by full is that it’ll work for anybody if the principals which might be outlined listed below are adopted. With out additional delay lets bounce proper into construct a dynamic RSS feed utilizing PHP and MySQL

To start our PHP primarily based RSS feed we have to do one little factor first. Check out your “.htaccess” file which is usually positioned within the root listing of net server. It’s the file that’s used for doing URL rewrites, 301 redirects and so on. Each Apache server ought to have it so search for it. If you’re utilizing dreamweaver a straightforward to get the file is to create it within the web site supervisor, i.e. right-click and create a brand new file. Rename the file “.htaccess” after which proper click on in your newly created file and click on “Get”. Once more that is just for customers of Dreamweaver. I’m making this extra difficult than it ought to be. Search for.htaccess in your net server root. Sufficient stated

As soon as we now have our.htaccess file discovered we have to make a change. Since our RSS file might be of the PHP extension i.e. rss.php and never of the xml sort. We’d like.htaccess to know that information with the sort.xml shoule be interpreted as.php information. To do that we enter the next in our.htaccess file:

AddType utility/x-httpd-php.xml

With our.htaccess file able to go we now want to start writing the PHP for our RSS feed. Our file might be damaged into 4 sections. The primary is the header which tells the browser that the file is of sort XML. The second part is the pinnacle of our RSS file. Its every part that you may make static in regards to the channel, i.e. the essential information associated to the RSS feed. Our third sections is the place it will get nifty. We create a database connection and use it to create our RSS feed. We then must loop by way of every of the gadgets we wish in our database and output them as xml. Lastly our fourth half is all of the output that’s required to shut the channel. There may be little to it except for just a few echo instructions however it’s a separate part in its personal minor manner.

First we have to create a brand new php file, I selected to call mine rss.php, you’ll be able to title yours no matter you want. As soon as we now have our file open we start with our first part:

//Set XML header for browser

header(‘Content material-type: textual content/xml’);

?>

Earlier than we do something we wish to ship a message to the browser that informs it that it’s coping with an XML file. If we didn’t move this data to the browser our dynamic RSS feed wouldn’t work. So simple as it sounds that’s it for our first a part of our RSS file.

In our subsequent part we have to start creating the construction of our XML file. There are few methods to do that. One is with the echo command. The opposite is thru using variables. I’ve used each and personally choose the variable technique because it appears easier in my thoughts. Subsequently that’s the technique I’ll display. Right here is our code (it consists of the code from our first part):

//Set XML header for browser

header(‘Content material-type: textual content/xml’);

//Create the heading/channel information for RSS Feed

$output = ”;

$output.= ”;

$output.= ”;

$output.= ‘Your RSS Feed Description’;

$output.= ‘ [url]’;

?>

Here’s what we did with the second part. First we created a variable $output. We set it equal to. Nonetheless as soon as we now have the worth set we don’t overwrite it. We as an alternative use the operator “.=” which suggests merely add to the present worth. So for instance if we stated that $some-variable = “a string”. We then used our “.=” operator so as to add to the worth like so $some-var.= ” like some var”. Subsequent if we had been to echo our variable $some-variable it could learn “a string like some var”. We’re taking our variable and including the entire xml tags to it to create one contiguous xml doc saved in a single variable, $output. One different factor to notice is that for the title, description and hyperlink tags you must add in your websites data. Whereas I do not assume anybody would deliberately use the data I had between the tags it’s straightforward to overlook little issues like that, which is why I point out it.

For our third part we get right down to the meat and bones of our php generated RSS web page. What we’re going to do is connect with a MySQL database and seize all of the pertinent data we want. Then we’re going to create particular person xml gadgets for every of the brand new entry or article that we now have. That is all achieved when the person accesses the RSS web page. Not earlier than. The web page is dynamic not static. For intents and functions we do not have an RSS web page till somebody accesses it. Now lets get to the code:

//Set XML header for browser

header(‘Content material-type: textual content/xml’);

//Create the heading/channel information for RSS Feed

$output = ”;

$output.= ”;

$output.= ”;

$output.= ‘Your RSS Feed Description’;

$output.= ‘ [url]’;

//Particular person Objects of our RSS Feed

//Hook up with a database and and show every new merchandise in our feed

//Hook up with DB

$host = “localhost”; //Title of host

$person = “cmsuser”; //Consumer title for Database

$move = “mypass”; //Password for Database

$db = “my_database”; //Title of Database

mysql_connect($host,$person,$move);

mysql_select_db($db);

//Create SQL Question for our RSS feed

$sql = “SELECT `title`, `hyperlink`, `description`, `date` FROM `articles`ORDER BY `date` DESC LIMIT zero, 15”;

$consequence = mysql_query($sql) or die (“Question could not be executed”);

//Create Loop for the person components within the RSS merchandise part

whereas ($row = mysql_fetch_array($consequence))

?>

Now a fantastic deal has occurred on this part so let me try to clarify every part clearly. First lets begin with the remark “Hook up with DB”. Right here we have to connect with a database. Usually I write a perform earlier on and easily name it after I wish to connect with a database and run a question. Nonetheless I can’t assume that you’ve already written one so we are going to undergo writing one collectively. First we outline variables that may home the mandatory data for the “mysql_connect” and “mysql_select_db” features. The data we have to retailer is our hostname, typically its “localhost”, our person title for the database, our password, and the title of the database. As soon as we now have that saved we use it within the perform “mysql_connect” which is used to make a connection a mysql database, as soon as we set up the connection we then want to pick out a database with the “mysql_select_db” assertion. Now that we now have related to our database lets study how we go about getting the knowledge we want.

Now that we’re related we should run a question to get the knowledge we want. For the instance I’ve made just a few assumptions the primary being that the title of the database is articles and that it comprises the 4 columns: `title`, `hyperlink`, `description`, `date` and that they’re named as such. I additionally restricted the consequence to 16, by utilizing the assertion “…LIMIT zero,15” which suggests solely present rows zero to 15. You possibly can set it to no matter you want to or you’ll be able to take away it fully and haven’t any restrict on the variety of entries in your RSS feed. Okay for small websites, terrible for giant ones. Use your discretion right here. Now that we now have the question constructed I wish to level out one factor. Usually you see folks utilizing the “SELECT *…” assertion after they run queries. Not solely do I feel its unhealthy observe however why get extra data that you just want, it takes longer and makes your web site run simply ever so barely slower. Subsequently I like to recommend that while you type your sql queries you implicitly state which fields you need moderately than utilizing a “SELECT *…” assertion. Now that we now have our question we want run it by utilizing the command “mysql_query” and move the outcomes right into a variable, cleverly referred to as $consequence. In case you discover that after our “mysql_query($sql)” assertion I’ve “or die(…)”. What that assertion does is that if there may be an error it kills the question and terminates the perform then echoes no matter error message you place within the brackets. Helpful for determining the place issues could go mistaken.

To this point we now have related to a database and run a question outputting the outcomes right into a variable, $consequence. Now we have to put this all right into a neat little RSS merchandise. To do that we have to create a loop, What our loop will do is undergo our question row by row and pull the knowledge from every row and do no matter we wish with it. On this case we wish to retailer it. To take action we create some time loop which mainly reads whereas there are rows nonetheless left in our consequence variable. We have to do no matter code is between the brackets . There are different methods to have shaped this loop however for now essentially the most direct technique is the one I’ve listed. Now that we now have our rows in a variable $row we have to add them to our xml file. To try this we use our good good friend “.=”and mainly add within the data for every merchandise we want to create. There are lots of extra tags you should utilize along with your RSS feed. I’ve solely chosen to make use of the “title”, “hyperlink”, “description”, “pubDate” since that was all I felt like i wanted and this isn’t an article on the construction of RSS however how one can generate them dynamically.

We now have accomplished three of our 4 steps. Since we now have all of our gadgets created, we did it beforehand with the whereas loop that may cycle by way of every consequence within the database and add it to our variable with the suitable tags, we have to end off our file and show it to the person. To do that we use the next code:

//Set XML header for browser

header(‘Content material-type: textual content/xml’);

//Create the heading/channel information for RSS Feed

$output = ”;

$output.= ”;

$output.= ”;

$output.= ‘Your RSS Feed Description’;

$output.= ‘ [url]’;

//Particular person Objects of our RSS Feed

//Hook up with a database and and show every new merchandise in our feed

//Hook up with DB

$host = “localhost”; //Title of host

$person = “cmsuser”; //Consumer title for Database

$move = “mypass”; //Password for Database

$db = “my_database”; //Title of Database

mysql_connect($host,$person,$move);

mysql_select_db($db);

//Create SQL Question for our RSS feed

$sql = “SELECT `title`, `hyperlink`, `description`, `date` FROM `articles`ORDER BY `date` DESC LIMIT zero, 15”;

$consequence = mysql_query($sql) or die (“Question could not be executed”);

//Create Loop for the person components within the RSS merchandise part

whereas ($row = mysql_fetch_array($consequence))

//Shut RSS channel

$output.= ”;

$output.= ”;

//Show output in browser

echo $output;

?>

Right here is our accomplished code in all its glory, all we now have added is 2 extra statements that append our variable with the next tags “” and “” which closes our channel and RSS tags respectively, that we opened earlier. After that we have to show the knowledge in order that the online browser can see it. To do that we merely use the echo command and echo our variable $output that we used to retailer all the knowledge beforehand. It is best to now be capable of see what I meant earlier within the article after I advised that one may use echo as an alternative of including the knowledge to a variable, however that is moreover the purpose. What issues now’s that you just received a totally working RSS feed that you just by no means need to toy with once more except its so as to add extra data. Hope you take pleasure in it and may put it to good use!

Source

Leave a Reply