I would appreciate some help. I am trying to create RSS feed for my website. I got half of it working, but I just want help with other half. I can't seem to add link to titles and I can't seem to be able to display a thumbnail in tag. Can someone help me please?
This is my code so far:
<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', '');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'testdb');
$connection = @mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
or die('Could not connect to database');
mysql_select_db(DB_NAME)
or die ('Could not select database');
$query = "SELECT teste.id, teste.title, teste.abstract, teste.body, teste.keywords, tesi.image FROM teste INNER JOIN tesi ON teste.title = tesi.title WHERE teste.title = tesi.title";
$result = mysql_query($query) or die ("Could not execute query");
$data = '<?xml version="1.0" encoding="UTF-8" ?>';
$data .= '<rss version="2.0">';
$data .= '<channel>';
$data .= '<title>Bang Premier</title>';
$data .= '<link>http://www.bangpremier.com</link>';
$data .= '<description>Entertainment News</description>';
while($row = mysql_fetch_array($result)) {
extract($row);
$data .= '<item>';
$data .= '<title>'.$row['title'].'</title>';
$data .= '<link>'.$row['image'].'</link>';
$data .= '<description>'.$row['abstract'].'</description>';
$data .= '</item>';
}
$data .= '</channel>';
$data .= '</rss> ';
header('Content-Type: application/xml');
echo $data;
?>
To debug the error look at the generated XML. For example open the source view in the browser. You should see something like:
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>Bang Premier</title>
<link>http://www.bangpremier.com</link>
<description>Entertainment News</description>
<item>
<title>Some Title</title>
<link>http://some.domain.tld/link</link>
<description>Some content</description>
</item>
</channel>
</rss>
Validate that the feed is valid wellformed XML and contains the data. The code in your question inserts the image
database field as link.
For this you should escape the values from the database using htmlspecialchars()
or even better use an XML Api like DOM or XMLWriter to generate the output.
RSS 2.0 feed items do not have an image
element - only the channel
.
You can include it as an img
element inside the description
:
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<item>
<title>Some Title</title>
<link>http://some.domain.tld/link</link>
<description>
<img src="http:/example.tld/images/thumbnail.png">
<br>
Some content
</description>
</item>
</channel>
</rss>
Some clients will edit the HTML however and may strip the img
.
Depending on your feed and the expected clients Media-RSS could be an option, too.