Forum Moderators: open
After a lot of thinking on what to have on my homepage, I have now decided on having a RSS feed from Google News besides a few articles, etc.
When I checked out Google feed, it is inside <rss>..</rss> tags. I would like to know how I can include them in my HTML page.
Also, should I enter the code inside a table so that it remains where I want it to be or is there some other procedure.
Thanks.
I build all my sites in php and so process the xml feed with php to produce the html that is viewed on the page. If you have a basic understanding of php you could implement the code in your page (provided your page extensions are .php not .html and your server has php). I'm happy to share.
In terms of placing it on your page it depends on whether you are using css or a tabled design. I exclusively use css and minimise tables altogether. In css you just put the feed in a div and define the width, position etc in the stylesheet.
<item>
<title></title>
<link></link>
<guid isPermaLink="false"></guid>
<pubDate></pubDate>
<description></description>
</item>
php has a series of functions for parsing xml (check out php.net). the code I use to parse xml feeds is below. It is not as good as some other peoples code, but it works for the feeds I use. you may need to modify the code for google news, I have not used google news feeds.
just enter the url for the feed in the spot where i have --enter url for news feed here-- and you can change the number of news items and the length of th description also easily.
<?$MAXLINKCOUNT = 10; // (* 2) if =10, 5 news items will show
$MAX_DESC_CHARS = 120;
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$linkcount = 0;
function startElement($parser, $name, $attrs) {
global $insideitem, $tag, $title, $description, $link;
if ($insideitem) {
$tag = $name;
} elseif ($name == "ITEM") {
$insideitem = true;
}
}
function endElement($parser, $name) {
global $insideitem, $tag, $title, $description, $link, $linkcount, $MAXLINKCOUNT, $MAX_DESC_CHARS;
if ($name == "ITEM" && $linkcount <= $MAXLINKCOUNT) {
if (strlen($description) > $MAX_DESC_CHARS) {
$description = wordwrap($description, $MAX_DESC_CHARS, "-=CUT OFF HERE=-");
$pos = strpos($description, "-=CUT OFF HERE=-");
$description = trim(substr($description, 0, $pos)) . "...";
}
$description = htmlspecialchars(trim($description));
$description = str_replace("&quot;", """, $description);
printf("<dt><b><a href='%s'>%s</a></b></dt>", trim($link),htmlspecialchars(trim($title)));
printf("<dt>%s</dt><br>",$description);
$title = "";
$description = "";
$link = "";
$insideitem = false;
}
}
function characterData($parser, $data) {
global $insideitem, $tag, $title, $description, $link, $linkcount;
if ($insideitem) {
switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "DESCRIPTION":
$description .= $data;
break;
case "LINK":
$link .= $data;
$linkcount++;
break;
}
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("--enter url for news feed here--","r")
or die("Error reading RSS data.");
while ($data = fread($fp, 2048)) {
xml_parse($xml_parser, $data, feof($fp));
}
fclose($fp);
xml_parser_free($xml_parser);
?>
I am not sure if it removes the html but if you want a clean rss feed from most major and international news sites you can try the ones over at newslookup in the feeds section.
The feed works really well for feeds that do not include html in the description tag. If the feed you want to use uses html in the description (like Google news) then you will need to edit the script.
Typical use is
require_once("magpierss/rss_fetch.inc");
$url = "http://rss.news.yahoo.com/rss/world";
$rss = fetch_rss($url);
Returned value $rss is an object with feed data and all items in an array. magpierss also has a built-in cache functionality.
I'm sure you must have seen on some sites specially project bidding sites where new project posted keep coming from below in side a box and old project move up.