http://www.phpwcms.de/forum/viewtopic.php?t=1596
===============================================
Okay here is what I did
NOTE: I commented out several functions for my USE
using the TAG {PHP:your_dir/with_rss_file_below.php}
save the following below as a php file.
I have commented out several lines, but if you uncomment this then you have extended functions.
change the following
"http://news.bbc.co.uk/rss/newsonline_uk ... rss091.xml" => "uk",
with the url of your rss feed, I have 2 feeds there, so you need to delete whichever for this note to be random.
note: this is an array for a random picker, see the file, and play with this.
note: this is not my coding, and might be improved in the future
I find this script works well, there are also no conflicts with the script...
enjoy
regards
TriP.
Code: Select all
<?php
/*
* PHP RSS parser 0.1
* ------------------
*
* RSS is RDF Site Summary. A technology used by netscape
* in their browser's channel function.
*
* This script gives you a PHP interface to read such
* feeds using the XML functions of PHP.
*
* Future changes:
* - A cachefunction to prevent putting heavy load on
* the server in the other end. Also to give a lot
* shorter loading time.
*
* Author: Christian Jørgensen
* http://www.razor.dk
* mail@phpguru.dk
*/
/*function select_box($array, $selected, $name) {
// This is my nifty selectbox-function :)
$out = "<select name=\"$name\">";
foreach ($array as $key => $value) $out .= sprintf("<option value=\"$key\"%s>$value</option>\n", $key == $selected ? ' selected' : '');
return $out."</select>";
}
*/
/*
* This is the dataarray containing the URLs and Names of the
* feeds to fetch. The URL goes as key.
*/
$backends2 = array(
"http://news.bbc.co.uk/rss/newsonline_uk_edition/england/rss091.xml" => "uk",
"http://newstrove.com/cgi-bin/rss.xml?search=austria&context_words=course+austrian+europe&max_results=20" => "at",
);
if (!$backend2) {
// If no backend is chosen, chose a random
srand((float) microtime() * 10000000);
$backend2 = array_rand($backends2);
}
// print "<h1>".$backends[$backend]."</h1>";
// The form containing the selectbox
// print "<form action='$PHP_SELF' method='POST'>";
// print select_box($backends, $backend, "backend");
// print " <input type='submit'>";
// print "</form>";
// Some vars used later on
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
$catagory = "";
function startElement2($parser, $tagName, $attrs) {
// The function used when an element is encountered
global $insideitem, $tag;
if ($insideitem) {
$tag = $tagName;
} elseif ($tagName == "ITEM") {
$insideitem = true;
}
}
function characterData2($parser, $data) {
// The function used to parse all other data than tags
global $insideitem, $tag, $title, $description, $link, $catagory;
if ($insideitem) {
switch ($tag) {
case "TITLE":
$title .= $data;
break;
case "DESCRIPTION":
$description .= $data;
break;
case "LINK":
$link .= $data;
break;
case "CATAGORY":
$catagory .= $data;
break;
}
}
}
function endElement2($parser, $tagName) {
// This function is used when an end-tag is encountered.
global $insideitem, $tag, $title, $description, $link, $catagory;
if ($tagName == "ITEM") {
printf("<b>• <a href='%s' target='_blank'>%s</a></b><br>",
trim($link),htmlspecialchars(trim($title)));
//printf("%s<br><br>",htmlspecialchars(trim($description)));
$title = $description = $link = $insideitem = $catagory = false;
}
}
// Now to the parsing itself. Starts by creating it:
$xml_parser = xml_parser_create();
// Then setup the handlers:
// CHange the Functions to match the new ones above
xml_set_element_handler($xml_parser, "startElement2", "endElement2");
xml_set_character_data_handler($xml_parser, "characterData2");
// Open the actual datafile:
$fp = fopen($backend2, r);
// Run through it line by line and parse:
while ($data = fread($fp, 4096)) {
xml_parse($xml_parser, $data, feof($fp))
or die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
// Close the datafile
fclose($fp);
// Free any memmory used
xml_parser_free($xml_parser);
?>