I’m trying to write a RSS feed for a website which mostly has Russian articles, though sometimes English. The site is built with php mysql.
This is what the text looks like when I visit the url:
Áåñåäà ¹1 èç öèêëà “Èçðàèëü è ìû”
What am I doing wrong?
Also, I’m tryn to add this line of code on top of the document but it breaks it and I get an error:
<?xml version="1.0" encoding="UTF-8"?>
Here is my php code excluding the database connect info.
I’m desperate. Please help.
<?php
require_once ('cutils/db_connect.php3');
// PHP file that renders perfect Dynamic XML for MySQL Database result sets
// Script written by Adam Khoury @ www.developphp.com - April 05, 2010
// View the video that is tied to this script for maximum understanding
// -------------------------------------------------------------------
header("Content-Type: rss-http;"); //set the content type to xml
// Initialize the xmlOutput variable
$xmlBody = '
<rss version="2.0">
<channel>
<title>Name of your site</title>
<description>A description of your site</description>
<link>http://yoururl.com/</link>
<copyright>Your copyright information</copyright>';
// Connect to your MySQL database whatever way you like to here
mysql_connect("localhost","dbuser","dbpass") or die (mysql_error());
mysql_select_db("sinaius2_em") or die ("no database");
// Execute the Query on the database to select items(20 in this example)
$sql = mysql_query("SELECT `id`, `title`, `article_text`, `article_date` FROM `articles`ORDER BY `article_date` DESC LIMIT 0 , 15");
mysql_query("SET NAMES utf8");
while($row = mysql_fetch_array($sql)){
// Set DB variables into local variables for easier use
$id = $row["id"];
$title = $row["title"] ;
$date_time = strftime("%b %d, %Y", strtotime($row["article_date"]));
$description = $row["article_text"];
// Start filling the $xmlBody variable with looping content here inside the while loop
// It will loop through 20 items from the database and render into XML format
$xmlBody .= "
<item>
<title>$title</title>
<description>TEST</description>
<pubDate>$date_time</pubDate>
<link>http://www.evreimir.com/article.php?id=$id</link>
</item> ";
} // End while loop
mysql_close(); // close the mysql database connection_aborted
$xmlBody .= "</channel>
</rss>";
echo $xmlBody; // output the gallery data as XML file for flash
?>
You can (and probably should) include the charset in your
Content-Typeheader. Also, I’ve never seen the content typerss-http, I’d use eithertext/xmlorapplication/xml. There also a few more specific content types, but there might be some issues with this. RSS is xml, so either generic xml content type will work fine.ISO-8859does include Cyrillic characters, but that may not be the charset your data is stored in here is a reference for various other Cyrillic charsets that are in-use. In any case, you should probably specify the same encoding that you’re using in your mysql database. You can useshow create table your_table_namein mysql to see what the charset is for a given table (or for an individual column in the table).Also… the description field in RSS will be handled as HTML after the user agent extracts if from the XML document. For this reason, I strongly suggest that you wrap the content of your item description as CDATA. Even so, you’ll still need to entity encode things that you would normally entity encode in HTML text (like a “less-than” symbol that’s not part of an html tag). Here’s a good example page.
Notice how the HTML
<b>tags don’t need encoding because they’re inside the CDATA, but the less-than symbol gets encoded anyway to escape it within the other HTML tags in the CDATA description. Without the CDATA, you’d need to xml-encode the contents of the description tag – which will require that the less-than in the above example actually be “double-encoded”. (so just use the CDATA, it’s much easier and less error-prone).