Forum Moderators: coopster

Message Too Old, No Replies

creating RSS with PHP error

         

jackvull

1:09 pm on Jan 29, 2007 (gmt 0)

10+ Year Member



I have created a feed with the following PHP.
Unfortunately, on validating the xml plenty of errors are returned including "not well formed".

Any ideas?

<?php

include("DBConnection.php");

// open a file pointer to an RSS file
$fp = fopen ("rss_mixes.xml", "w");

// Now write the header information
fwrite ($fp, "<?xml version='1.0'?><rss version='2.0'><channel>");

fwrite ($fp, "<title>Tester</title>");

fwrite ($fp, "<link>http://www.example.co.uk/</link>");

fwrite ($fp, "<description>Tester</description>");

fwrite ($fp, "<language>en-us</language>");

fwrite ($fp, "<docs>http://www.example.co.uk/rss_mixes.xml</docs>");

$sSQL = "SELECTdistinct mixname,
Mixes.DJ
FROMMixes
WHEREMixes.Visible = 1
ORDER BYMixes.MixID DESC";

$content_result = mysql_query($sSQL) or trigger_error(mysql_error());

while ($content_rec = mysql_fetch_row($content_result)) {
fwrite ($fp, "<item>");

$headline = $content_rec[0];
$content_1 = substr($content_rec, 0, 250);
$content = strip_tags($content_1);
if (strlen($content_rec[1]) > 250) {
$content = $content . "....";
}
fwrite ($fp, "<title>$headline</title>");
fwrite ($fp, "<description>Mixed by $content</description>");
$item_link = "http://www.example.co.uk/Mixes/$content_rec[3]";
fwrite ($fp, "<link>$item_link</link>");

fwrite ($fp, "</item>");
}

fwrite ($fp, "</channel></rss>");
fclose ($fp);

?>

[1][edited by: bill at 2:35 am (utc) on Feb. 2, 2007]
[edit reason] Use "example" for domain name [/edit]

bill

3:47 am on Feb 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Could anyone in the PHP forum help out with this? We didn't have much luck in the RSS forum.

eelixduppy

4:05 am on Feb 2, 2007 (gmt 0)



I don't see anything wrong with the PHP except for this one line:

$item_link = "http://www.example.co.uk/Mixes/$content_rec[3]";

Which isn't properly constructed. It should be either one of the following:


$item_link = "http://www.example.co.uk/Mixes/{$content_rec[3]}";
# or
$item_link = "http://www.example.co.uk/Mixes/".$content_rec[3];

If you are having trouble validating the XML part, and aren't getting any PHP errors, then I'd suggest you run the script, then view the resulting xml file for xml-specific errors. I am not too familiar with xml validation, so I'll let someone else take it from here :)

Good luck!

eelixduppy

5:02 am on Feb 2, 2007 (gmt 0)



Another quick skim turned up another error. The error is in this line:

$content_1 = substr($content_rec, 0, 250);

Can you spot it? ;) $content_rec is an array here, not a string. I think you were meaning to make it something like this:


#Now it is referencing an element of the array; not the array itself.
$content_1 = substr([b]$content_rec[1][/b], 0, 250);

Now let's see if we can spot some xml errors :)