Page 1 of 1

Creating dynamic RSS feed with PHP?

Posted: Mon Jan 05, 2009 4:09 pm
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.

Re: Creating dynamic RSS feed with PHP?

Posted: Mon Jan 05, 2009 4:15 pm
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.

Re: Creating dynamic RSS feed with PHP?

Posted: Tue Jan 06, 2009 12:22 pm
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>