PHP to pull rss and xml newsfeeds

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
webweever
Forum Newbie
Posts: 7
Joined: Sun May 16, 2004 9:56 pm

PHP to pull rss and xml newsfeeds

Post by webweever »

I'm looking for a PHP snippet to pull rss and xml newsfeeds. I'm looking for the simplest possible, I'm no PHP expert.

Any ideas?
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

User avatar
webweever
Forum Newbie
Posts: 7
Joined: Sun May 16, 2004 9:56 pm

Post by webweever »

I found this:

Code: Select all

<?php

class xItem {
var $xTitle;
var $xLink;
var $xDescription;
}

// general vars
$sTitle = "";
$sLink = "";
$sDescription = "";
$arItems = array();
$itemCount = 0;

// ********* Start User-Defined Vars ************
// rss url goes here
$uFile = "http://rssnewsapps.ziffdavis.com/tech.xml";
// descriptions (true or false) goes here
$bDesc = true;
// font goes here
$uFont = "Verdana, Arial, Helvetica, sans-serif";
$uFontSize = "2";
// ********* End User-Defined Vars **************

function startElement($parser, $name, $attrs) {
global $curTag;

$curTag .= "^$name";

}

function endElement($parser, $name) {
global $curTag;

$caret_pos = strrpos($curTag,'^');

$curTag = substr($curTag,0,$caret_pos);

}

function characterData($parser, $data) { global $curTag; // get the Channel information first
global $sTitle, $sLink, $sDescription; 
$titleKey = "^RSS^CHANNEL^TITLE";
$linkKey = "^RSS^CHANNEL^LINK";
$descKey = "^RSS^CHANNEL^DESCRIPTION";
if ($curTag == $titleKey) {
$sTitle = $data;
}
elseif ($curTag == $linkKey) {
$sLink = $data;
}
elseif ($curTag == $descKey) {
$sDescription = $data;
}

// now get the items 
global $arItems, $itemCount;
$itemTitleKey = "^RSS^CHANNEL^ITEM^TITLE";
$itemLinkKey = "^RSS^CHANNEL^ITEM^LINK";
$itemDescKey = "^RSS^CHANNEL^ITEM^DESCRIPTION";

if ($curTag == $itemTitleKey) {
// make new xItem 
$arItems[$itemCount] = new xItem(); 

// set new item object's properties 
$arItems[$itemCount]->xTitle = $data;
}
elseif ($curTag == $itemLinkKey) {
$arItems[$itemCount]->xLink = $data;
}
elseif ($curTag == $itemDescKey) {
$arItems[$itemCount]->xDescription = $data;
// increment item counter
$itemCount++;
}
}

// main loop
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($uFile,"r"))) {
die ("could not open RSS for input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);


// write out the items
?>
<html>
<head>
<title><?php echo ($sTitle); ?></title>
<meta name = "description" content = "<?php echo ($sDescription); ?>">
</head>
<body bgcolor = "#FFFFFF">
<font face = "<?php echo($uFont); ?>" size = "<?php echo($uFontSize); ?>"><a href = "<?php echo($sLink); ?>"><?php echo($sTitle); ?></a></font>
<br>
<br>
<?php
for ($i=0;$i<count($arItems);$i++) {
$txItem = $arItems[$i];
?>
<font face = "<?php echo($uFont); ?>" size = "<?php echo($uFontSize); ?>"><a href = "<?php echo($txItem->xLink); ?>"><?php echo($txItem->xTitle); ?></a></font>
<br>
<?php
if ($bDesc) {
?>
<font face = "<?php echo($uFont); ?>" size = "<?php echo($uFontSize); ?>"><?php echo ($txItem->xDescription); ?>
<br>
<?php
}
echo ("<br>");
}
?>
But I would like to change it to pull more than one feed and lose all the formatting, I want to pull it as it is on the target server.

Any ideas?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

This is quite a nice article on how to do that. Also, have a look at the bottom of this article.
User avatar
webweever
Forum Newbie
Posts: 7
Joined: Sun May 16, 2004 9:56 pm

Post by webweever »

That works, thanks for the links. One more thing, I didnt see anywhere in those article how to pull 2 different RSS feeds at the same time? Any idea how to do that?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

RSS-feeds are basically a standard-compliant XML-document. You can parse them cumulative, keep the parsed results from the first in an array and then parse the second one and then combine them.
The benefit of that is, that you can use PHP's array-functions on them, if necessary. Or, if you're an XLST expert, you can do all that with XLST.
User avatar
webweever
Forum Newbie
Posts: 7
Joined: Sun May 16, 2004 9:56 pm

Post by webweever »

Any idea how I would code that or an example? I'm a PHP rookie to say the least.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

if you're not using XSLT

Code: Select all

<?php

// I assume you have a class which fetches and parses the RSS-feed.
// and further assume that one method there is called ->fetch()
// to avoid errors: if something is wrong (rss-file not there, server down 
//  etc. etc.), have it  return an empty array;

include_once("rss_parse_class.inc.php");
$rss			=	&new rss_parse_class;

$rss_source		=	array(	"http://www.bbc.co.uk/syndication/feeds/news/ukfs_news/world/rss091.xml",
							"http://www.zend.com/rss.php?type=art",
							"http://www.phppatterns.com/rss/"
						)
						
$rss_parsed		=	array();				

foreach($rss_source	as $rss_location){
	$rss_parsed	=	array_merge_recursive($rss_parsed,$rss->fetch($rss_location));
}

// now display the what we got
echo "<h3>RSS PARSED</h3><pre>";
print_r($rss_parsed);
echo"</pre>";


?>
I haven't really tested that script (work is demanding...;)) - but I hope you get the gist of it. If not: let me know :)
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

this handles multiple feeds quite easily...
http://www.geckotribe.com/rss/carp/
User avatar
webweever
Forum Newbie
Posts: 7
Joined: Sun May 16, 2004 9:56 pm

Post by webweever »

I have no rss_parse_class.inc.php file, just the one I posted earlier. Will your script work without the rss_parse_class.inc.php file?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Nope. I assumed you already had downloaded a class doing that. "rss_parse_class.inc.php" is only a name for a class, you could use any name.

Maybe worthwhile exploring http://pear.php.net/package/XML_RSS
User avatar
webweever
Forum Newbie
Posts: 7
Joined: Sun May 16, 2004 9:56 pm

Post by webweever »

Other than the file I already have what would a class file contain?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

If properly programmed, it would contain a clean API with which you can interface. The script you have there is quite messy, content and logic is heavily mixed, unnecessary use of globals etc. etc..
I would really advise using the PEAR-class I linked to.
Post Reply