Jump to content

need help


Guest

Recommended Posts

Hi

yes im building a new site and need to display an rss feed on it wots an easy way to do this?

I have the url for the feed and would like it to appear in like an iframe (maybe) or something like that so its within an html template.

thanks in advance <_<

Link to comment
Share on other sites

Guest jeromas

I used the following code on one of my in-development sites, http://www.dnaguardian.com/index.php

<?php

class RSSParser {

var $insideitem = false;

var $tag = "";

var $title = "";

var $description = "";

var $link = "";

var $XML_parsed = '';

function startElement($parser, $tagName, $attrs) {

if ($this->insideitem) {

$this->tag = $tagName;

} elseif ($tagName == "ITEM") {

$this->insideitem = true;

}

}

function endElement($parser, $tagName) {

if ($tagName == "ITEM") {

//printf("<dt><b><a href='%s'>%s</a></b></dt>",

// trim($this->link),htmlspecial=chars(trim($this->title)));

//printf("<dd>%s</dd>",htmlspecialchars(trim($this->description)));

$this->XML_parsed .= html_entity_decode('<br /><br /><a target="_blank" href="'.trim($this->link).'"> '.htmlspecialchars(trim($this->title)).'</a><br />'.htmlspecialchars(trim($this->description)));

$this->title = "";

$this->description = "";

$this->link = "";

$this->insideitem = false;

}

}

function getParsedXML(){

return $this->XML_parsed;

}

function characterData($parser, $data) {

if ($this->insideitem) {

switch ($this->tag) {

case "TITLE":

$this->title .= $data;

break;

case "DESCRIPTION":

$this->description .= $data;

break;

case "LINK":

$this->link .= $data;

break;

}

}

}

}

/* Mod to allow channel changing - by Jerry Higbee */

switch($_POST['channel']){

case 'Current Amber Alerts':

/* Americas Amber Alert Center National */

$channel = 'http://www.teamamberalert.net/news/backend.php';

break;

case 'Missing Children Alerts':

/* Missing Children Alert Cases */

$channel = 'http://www.missingkids.com/missingkids/servlet/XmlServlet?act=rss';

break;

case 'Nationwide Safety Network':

/* Sexual Offenders - The Nationwide Safety Network */

$channel = 'http://www.sexualoffenders.com/sexualoffenders.rss';

break;

case 'Consumer Safety':

/* US Consumer Product Safety Commission */

$channel = 'http://www.cpsc.gov/cpscpub/prerel/prerel.xml';

break;

case 'MedicineNet Child Abuse News':

/* MedicineNet Child Abuse Specialty */

$channel = 'http://www.medicinenet.com/rss/specialty/child_abuse.xml';

break;

case 'CBS News':

/* CBS News Top Stories */

$channel = 'http://www.cbsnews.com/feeds/rss/main.rss';

break;

case 'ABC News':

/* ABC News Top Stories */

$channel = 'http://my.abcnews.go.com/rsspublic/fp_rss20.xml';

break;

case 'MSNBC News':

/* MSNBC News Top Stories */

$channel = 'http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml';

break;

case 'Fox News':

/* Fox News Top Stories */

$channel = 'http://www.foxnews.com/xmlfeed/rss/0,4313,1,00.rss';

break;

case 'CNN':

/* CNN News Top Stories */

$channel = 'http://rss.cnn.com/rss/cnn_us.rss';

break;

}

/* End mod to allow channel changing */

$xml_parser = xml_parser_create();

$rss_parser = new RSSParser();

xml_set_object($xml_parser,&$rss_parser);

xml_set_element_handler($xml_parser, "startElement", "endElement");

xml_set_character_data_handler($xml_parser, "characterData");

$fp = fopen($channel,"r")

or die("Error reading RSS data.");

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)));

fclose($fp);

// Assign resulting parsed XML to a variable

$headlines = $rss_parser->getParsedXML();

$index->assign("CHANNEL_HEADLINES", $headlines);

xml_parser_free($xml_parser);

?>

As you can see there is a mod in there by me that allows a person to 'change the channel' to select a different feed. If you don't want this ability, feel free to comment out that code, otherwise you'll need to add a form field for the $_POST variable on whatever page you are placing the feed on.

If you'd like more explanation about how I implemented it on the page, please feel free to PM me.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...