Creating dynamic RSS feed with PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zachalbert
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2009 11:32 pm

Creating dynamic RSS feed with PHP?

Post by zachalbert »

So I have a bunch of content in a database, and a php file that pulls one piece out per day specific to the day (meaning, not random).

I want to turn the php into an RSS feed so that if subscribed, you would receive the one piece of data, daily.

I can create the xml tags with php, but what about the format? Can I run php code in an xml file, or vice versa?

And assuming I can run php code in the xml file, how do I get it to update daily? Once I finish it, it is going to be completely automated. Do I have to run the file daily so it updates, or do the RSS readers handle that?

Sorry if these are dumb questions but I haven't had much luck finding the answers elsewhere.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Creating dynamic RSS feed with PHP?

Post by Syntac »

If you're just embedding the code in the file, it's pretty simple. Chances are you'll have to use the extension .php, though, so try setting the header "Content-type: application/rss+xml".

Since the content will always be up to date using this method, you'll never have to run the file yourself — the RSS reader will take care of that.
zachalbert
Forum Newbie
Posts: 13
Joined: Sun Jan 04, 2009 11:32 pm

Re: Creating dynamic RSS feed with PHP?

Post by zachalbert »

So it worked completely fine in testing, but in practice in never changes. In the code below, notice the start date below is set to 01-05-2009 (which gives you article 1). When I change that to 01-01-2009 for instance, it would correctly output article 5.

However, I put the feed on my server, subscribed to it to test it out, waited a day, and it's still yet to change. I still only see the first article, but when I view source, it shows <lastBuildDate> as the 6th. I get lastBuildDate with date('r'), and the current date below with ('z'). Why is one date updating and the other not?

Code: Select all

 
<?php
header("Content-Type: application/xml; charset=UTF-8"); 
$xmlV = "<?xml version=\"1.0\"?>";
echo $xmlV; // because it was parsing the <?xml as php
?>
<rss version="2.0">
<channel>
    ... all the channel stuff ...
    <lastBuildDate> <?php echo date('r'); ?> </lastBuildDate>
 
<?php
// Start date below
$Sday = '05';
$Smonth = '01';
$Syear = '2009';
 
// Current date. This is not updating, as far as I can tell
$Cday = date('z')+1;
$Cmonth = date('m');
$Cyear = date('Y');
 
// Determine number of days since start date and which article number we should be on
$Pday = $Cday - $Sday;
$Pmonth = $Cmonth - $Smonth;
$Pyear = ($Cyear - $Syear) * 365;
$Passed = $Pday + $Pyear; // # of days since start date
$Tarticle = ($Passed % 383); // article number out of 383, based on the number of days passed
 
require('connect.php');
 
$query = "SELECT * FROM test_table WHERE id='".$Tarticle."'";
 
$result1 = mysql_query($query) or die(mysql_error());
$result2 = mysql_query($query) or die(mysql_error());
$header = mysql_fetch_array($result2);
 
$sectionNum = $header['section'];
$sectionTxt = $header['sectiontxt'];
 
while ( $row = mysql_fetch_array( $result1 ) ) {
    $textNum = $row['number'];
    $textTxt = $row['text'];
 
echo "<item>\n";
echo "<title>".$sectionNum.". ".$sectionTxt." - #".$textNum."</title>\n";
echo "<link> http://www.link.com#".$sectionNum."-".$textNum." </link>\n";
echo "<guid> http://www.link.com#".$sectionNum."-".$textNum." </guid>\n";
echo "<description>\n";
echo $textNum.". ".$textTxt."\n";
echo "</description>\n";
echo "</item>\n\n";
 
}
?>
</channel>
</rss>
 
Post Reply