...

View Full Version : Using PHP to generate and XML RSS Feed



madmatter23
03-08-2008, 02:28 AM
Hello,

I'm using a PHP page to draw information from an SQL database and generate and RSS feed via XML. Lots of acronyms huh?

Anyway, I've pretty much finished the whole thing... it's just not working. I'm not sure why.

Essentially, when you view the xml page, none of the items show up. But when viewing the source, everything looks right. The xml seems to be generated correctly, so I'm not sure what's causing the issue.

Here's the php


<?php
include "phpdb/dbaccess.php";

header( 'Content-Type: text/xml' );
echo "<?xml version='1.0' ?>
<?xml-stylesheet href='rss.xsl' type='text/xsl' media='screen' ?>
<rss version='2.0'>
<channel>
<title>Turning the Tide</title>
<language>en-us</language>
<link>http://www.maggiepascal.com/</link>
<description>Turning the Tide</description>
<webMaster>matt@knowideaproductions.com</webMaster>
";

$timeConvert = array (
"01:00:00" => "1am", "02:00:00" => "2am", "03:00:00" => "3am", "04:00:00" => "4am", "05:00:00" => "5am", "06:00:00" => "6am", "07:00:00" => "7am", "08:00:00" => "8am", "09:00:00" => "9am", "10:00:00" => "10am", "11:00:00" => "11am", "12:00:00" => "12pm", "13:00:00" => "1pm", "14:00:00" => "2pm", "15:00:00" => "3pm", "16:00:00" => "4pm", "17:00:00" => "5pm", "18:00:00" => "6pm", "19:00:00" => "7pm", "20:00:00" => "8pm", "21:00:00" => "9pm", "22:00:00" => "10pm", "23:00:00" => "11pm", "24:00:00" => "12am"
);

$query = "SELECT * FROM tblShow, tblGuest WHERE tblShow.fk_tblGuest = tblGuest.pk_tblGuest ORDER BY showdate DESC,starttime DESC";
$result = mysql_query($query) or die("Error, the server encountered an error:".mysql_error());
while ($r = mysql_fetch_assoc($result)){

$showDateArray = explode("-",$r['showdate']);
$showDateDisplay= date('M d, Y', mktime(0,0,0,$showDateArray[1],$showDateArray[2],$showDateArray[0]));

$firstname = preg_replace('/(\\\")/',"&quot;", $r['firstname']);
$firstname = preg_replace("/(\\\')/","&rsquo;", $firstname);
$lastname = preg_replace('/(\\\")/',"&quot;", $r['lastname']);
$lastname = preg_replace("/(\\\')/","&rsquo;", $lastname);
$topic = preg_replace('/(\\\")/',"&quot;", $r['topic']);
$topic = preg_replace("/(\\\')/","&rsquo;", $topic);

if ($r['prefix'] == NULL && $r['firstname'] == NULL && $r['lastname'] == NULL){
$guestName = "No Guest";
}
else{
$guestName = $r['prefix']." ".$r['firstname']." ".$r['lastname'];
}


echo"
<item>
<title>$guestName</title>
<description>$topic</description>";

if ($showInfo['audio'] != NULL){
$audioSize = filesize("http://www.maggiepascal.com/{$r['audio']}");
echo "\n <link>http://www.maggiepascal.com/{$r['audio']}</link>";
// <enclosure url='http://www.maggiepascal.com/{$r['audio']}' length='$audioSize' type='audio/mpeg' />
}
if ($showInfo['video'] != NULL){
$videoSize = filesize("http://www.maggiepascal.com/{$r['video2']}");
echo "\n <link>http://www.maggiepascal.com/watch?showid={$r['pk_tblShow']}</link>";
// <enclosure url='http://www.maggiepascal.com/watch?showid={$r['pk_tblShow']}' length='$videoSize' type='video/swf' />
}
if ($showInfo['video2'] != NULL){
$video2Size = filesize("http://www.maggiepascal.com/{$r['video2']}");
echo "\n <link>http://www.maggiepascal.com/watch?showid={$r['pk_tblShow']}&segment=2</link>";
}
if ($showInfo['video3'] != NULL){
$video4Size = filesize("http://www.maggiepascal.com/{$r['video4']}");
echo "\n <link>http://www.maggiepascal.com/watch?showid={$r['pk_tblShow']}&segment=3</link>";
}
if ($showInfo['video4'] != NULL){
$video3Size = filesize("http://www.maggiepascal.com/{$r['video3']}");
echo "\n <link>http://www.maggiepascal.com/watch?showid={$r['pk_tblShow']}&segment=4</link>";
}

echo "
<pubDate>$r{$showdate}</pubDate>
<time>{$r['starttime']}</time>
</item>
";
}

echo "
</channel>
</rss>
";
?>


You can visit http://www.maggiepascal.com/podcast.php to see the page and view its source.

If anyone has any ideas, I'd really appreciate the help. I expect that the problem must be something subtle...

I should also add that I'm trying to add up to 4 video links to each item. This is because the video's are often split into 3-4 segments. I'm not sure that I'm going about it the right way.

mlseim
03-08-2008, 05:58 AM
I think your problem is in the top portion of the script.
I took a snippet of code from another XML script.

Try this replacement for the first part of your script ...



<?php
include "phpdb/dbaccess.php";

// Begin feed
header ("Content-Type: text/xml; charset=ISO-8859-1");
echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
?>
<rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:admin="http://webns.net/mvcb/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<channel>
<title>Turning the Tide</title>
<language>en-us</language>
<link>http://www.maggiepascal.com/</link>
<description>Turning the Tide</description>
<webMaster>matt@knowideaproductions.com</webMaster>

<?php
$timeConvert = array (
"01:00:00" => "1am", "02:00:00" => "2am", "03:00:00" => "3am", "04:00:00" => "4am", "05:00:00" => "5am", "06:00:00" => "6am", "07:00:00" => "7am", "08:00:00" => "8am", "09:00:00" => "9am", "10:00:00" => "10am", "11:00:00" => "11am", "12:00:00" => "12pm", "13:00:00" => "1pm", "14:00:00" => "2pm", "15:00:00" => "3pm", "16:00:00" => "4pm", "17:00:00" => "5pm", "18:00:00" => "6pm", "19:00:00" => "7pm", "20:00:00" => "8pm", "21:00:00" => "9pm", "22:00:00" => "10pm", "23:00:00" => "11pm", "24:00:00" => "12am"
);

. the rest of your script
.
.
.

madmatter23
03-08-2008, 06:57 AM
No luck with the new header, thanks for the advice though. I have noticed that the feed actually does work in safari, just not in firefox... It's tough to know where to start with the problem since there is no error message. Any other ideas?

Thanks!

mlseim
03-08-2008, 06:32 PM
dumb it down ...

Try making a simple XML file without any database stuff.
Just use a few simple tags and some "jibberish" text for testing.

See if you can make a simple XML file work before including
all of your database information. That might trigger some ideas.

oesxyl
03-08-2008, 07:29 PM
I hope I don't missunderstand your problem.
You can't just echo an xml file becouse of special chars, try this:



echo "<pre>".htmlspecialchar($xml)."</pre>";


here I asume you have in $xml the content of your xml.

best regards

madmatter23
03-09-2008, 07:08 PM
Thanks for the advice, I think that I'm a lot closer to having it work.

I changed the header tags, ran most of the xml through htmlspecialchars(), and now I'm just having trouble with the filesize() command.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum