here goes:
Code:
<?php
function start_element($xml_parser,$name,$attr)
{
global $inside_item, $tag;
if($inside_item) $tag=$name;
else if($name=="ITEM") $inside_item=true;
}
function end_element($xml_parser,$name)
{
global $inside_item, $tag, $title, $description, $link, $stories;
if($name=="ITEM") {
/*This is the bit you change to suit your display*/
$stories[]="
<h2><a target='_blank' href='".$link."'>".$title."</a></h2>
<p>".$description."</p><br />
<hr />
<br />
";
$title="";
$link="";
$description="";
$inside_item=false;
}
}
function inside_element($xml_parser,$data)
{
global $inside_item, $tag, $title, $description, $link;
if($inside_item) {
switch($tag) {
case "TITLE":
$title.=$data;
break;
case "DESCRIPTION":
$description.=$data;
break;
case "LINK":
$link.=$data;
break;
}
}
}
//--------end xml functions---------
function rss_feed($provider, $view = "all")
{
//variables
global $inside_item, $tag, $title, $description, $link, $stories;
$inside_item = false;
$title = "";
$description = "";
$link = "";
$tag = "";
$stories = array();
//create parser
$parser = xml_parser_create();
xml_set_element_handler($parser, "start_element", "end_element");
xml_set_character_data_handler($parser, "inside_element");
//read xml file
if($fh=@fopen($provider, "rb")) {
while($fdata=fread($fh, 4096)) {
if(!@xml_parse($parser, $fdata,feof($fh))) {
$failed = true;
return false;
}
}
} else {
$failed=true;
return false;
}
fclose($fh);
xml_parser_free($parser);
//output
if(!$failed) {
if($view=="r") {
return $stories[rand(0,(count($stories)-1))];
} else if($view=="1") {
return $stories[0];
} else {
$output="";
foreach($stories as $key=>$story) {
$output.=$story;
}
return $output;
}
}
}
?>
paste the above code into the page you want to use the feed (or in a functions file that is included from every page or something)
Then to get the output you do this:
Code:
$ut = rss_feed("http://www.universetoday.com/universetoday.xml");
echo $ut;
to output all the stories. Change the bit I've commented in the end_element() function to change the look of each story. You can also call the rss_feed function with a second argument to give one story chosen at random:
Code:
$ut = rss_feed("http://www.universetoday.com/universetoday.xml","r");
echo $ut;
or just the top story:
Code:
$ut = rss_feed("http://www.universetoday.com/universetoday.xml","1");
echo $ut;
As for images, you'll have to ask fraser about that. I would like to see the image urls included as well, but its up to him.
Incidentally you can use the function for any feed that uses the same format, just call the function with the appropriate URL address of the feed.