PHP RSS SCRIPT

Hello everyone,

I was looking for short simple script to show rss feeds on my website
and I found this one and it works awesome except I don't know what to
add or modify to limit the number of entries it shows. The entire script
is posted below. Thanks in advance for the help.

<?php
/*

Class RSSParser: 2 October 2002
Original Author: Duncan Gough
Overview: An RSS parser that uses PHP and freely available RSS/RDF/XML
feeds
to add fresh news content to your site.
Installation: Decompress the file into your webroot and include it from
whichever pages on which you want to display the data, e.g;
include("rss.php"); Hackers Note: can also be called as SSI
from a .shtml page by using: <!--include virtual="rss.php"-->
Usage: As above, just include the rss.php file from within your PHP page,
and the news will appear. You should find the HTML code in the
functions endElement(), show_title() and show_list_box() below,
feel free to modify these to match your site.
Single Feed Hack: By Gregg 't3h GeeK' van der Sluys,
http://geek.scorpiorising.ca
12-13-03. Permission granted from Duncan Gough to re-release under
the GPL. This code will output raw text and will validate as XHTML 1.0
Transitional.
Bugs graciously worked out by Russell Najar, http://www.sicomm.us
02-28-06
*/

class RSSParser {

var $title = "";
var $link = "";
var $description = "";
var $inside_item = false;

// This is an example default that I chose to display on my site -
http://geek.scorpiorising.ca/
// Add new RSS feeds using this format;
// "http://www.wherever.com/path/to/rss/" => "Display name",
var $all_rss_urls = array(
"http://freshmeat.net/backend/fm.rdf" => "Freshmeat.net",
);

function startElement( $parser, $name, $attrs='' ){
global $current_tag;

$current_tag = $name;

if( $current_tag == "ITEM" )
$this->inside_item = true;

} // endfunc startElement

function endElement( $parser, $tagName, $attrs='' ){
global $current_tag;

if ( $tagName == "ITEM" ) {

printf( "\t
<b><a href='%s' target='_blank'>%s</a></b>\n",
$this->link , $this->title);
printf( "\t
%s
\n", $this->description);

$this->title = "";
$this->description = "";
$this->link = "";
$this->inside_item = false;

}

} // endfunc endElement

function characterData( $parser, $data ){
global $current_tag;

if( $this->inside_item ){
switch($current_tag){

case "TITLE":
$this->title .= $data;
break;
case "DESCRIPTION":
$this->description .= $data;
break;
case "LINK":
$this->link .= $data;
break;

default:
break;

} // endswitch

} // end if

} // endfunc characterData

function parse_results( $xml_parser, $rss_parser, $file ) {

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("$file","r") or die( "Error reading XML file, $file" );

while ($data = fread($fp, 4096)) {

// parse the data
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 ) ) );

} // endwhile

fclose($fp);

xml_parser_free( $xml_parser );

} // endfunc parse_results

function show_title( $rss_url ){
?>
<small></small>
<?
} // endfunc show_title

} // endclass RSSParser

global $rss_url;

// Set a default feed
if( $rss_url == "" )
$rss_url = "http://freshmeat.net/backend/fm.rdf";

$xml_parser = xml_parser_create();
$rss_parser = new RSSParser();

$rss_parser->show_title( $rss_url );
$rss_parser->parse_results( $xml_parser, &$rss_parser, $rss_url );

?>
zach [ Do, 26 April 2007 03:29 ] [ ID #1698422 ]

Re: PHP RSS SCRIPT

"zach" <wackzingo [at] gmail.com> wrote in message
news:F7SdnewfZeP1na3bnZ2dnUVZ_gCdnZ2d [at] wavecable.com...
> Hello everyone,
>
> I was looking for short simple script to show rss feeds on my website
> and I found this one and it works awesome except I don't know what to
> add or modify to limit the number of entries it shows. The entire script
> is posted below. Thanks in advance for the help.
>
You could count the number of </item>'s according to the limit you'd impose.
Grab a substring up to that point [including the </item> tag] and append
</channel></rss> to the substring, thereby recreating a shortened version.
I think ;)
Vince
Vince Morgan [ Do, 26 April 2007 10:00 ] [ ID #1698425 ]

Re: PHP RSS SCRIPT

"zach" <wackzingo [at] gmail.com> wrote in message
news:F7SdnewfZeP1na3bnZ2dnUVZ_gCdnZ2d [at] wavecable.com...
> Hello everyone,
>
> I was looking for short simple script to show rss feeds on my website
> and I found this one and it works awesome except I don't know what to
> add or modify to limit the number of entries it shows. The entire script
> is posted below. Thanks in advance for the help.
>
funcion chopRss($fileIn, $fileOut, $noItems)
{
$reRss=file_get_contents($fileIn);
$ary=spliti('<item>', $reRss);
$reRss='';
$i=0;
while($i < $noItems)
{
$reRss.=$ary[$i];
++$i;
}
$reRss.='</channel></rss>';
file_put_contents($fileOut, $reRss);
)

Perhaps there is a better solution, not tested but I don't think it has any
errors.
HTH
Vince
Vince Morgan [ Do, 26 April 2007 11:29 ] [ ID #1698426 ]

Re: PHP RSS SCRIPT

"Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
news:463070bb$0$5743$afc38c87 [at] news.optusnet.com.au...
>
> "zach" <wackzingo [at] gmail.com> wrote in message
> news:F7SdnewfZeP1na3bnZ2dnUVZ_gCdnZ2d [at] wavecable.com...
> > Hello everyone,
> >
> > I was looking for short simple script to show rss feeds on my website
> > and I found this one and it works awesome except I don't know what to
> > add or modify to limit the number of entries it shows. The entire script
> > is posted below. Thanks in advance for the help.
> >
Too hurried sorry. Made an error

> funcion chopRss($fileIn, $fileOut, $noItems)
First param is the name of the rss file you want to chop, second is the file
to write the results to, and the last is the number of items you want.

function chopRss($fileIn, $fileOut, $noItems=1)
{
$reRss=file_get_contents($fileIn);
$ary=spliti('<item>', $reRss);
$limit=$noItems;
$reRss='';
$i=0;
while($i < $noItems)
{
$reRss.=$ary[$i];
++$i;
}
$reRss.='</channel></rss>';
file_put_contents($fileOut, $reRss);
)
Vince Morgan [ Do, 26 April 2007 11:34 ] [ ID #1698427 ]

Re: PHP RSS SCRIPT

Vince Morgan wrote:
> "Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
> news:463070bb$0$5743$afc38c87 [at] news.optusnet.com.au...
>> "zach" <wackzingo [at] gmail.com> wrote in message
>> news:F7SdnewfZeP1na3bnZ2dnUVZ_gCdnZ2d [at] wavecable.com...
>>> Hello everyone,
>>>
>>> I was looking for short simple script to show rss feeds on my website
>>> and I found this one and it works awesome except I don't know what to
>>> add or modify to limit the number of entries it shows. The entire script
>>> is posted below. Thanks in advance for the help.
>>>
> Too hurried sorry. Made an error
>
>> funcion chopRss($fileIn, $fileOut, $noItems)
> First param is the name of the rss file you want to chop, second is the file
> to write the results to, and the last is the number of items you want.
>
> function chopRss($fileIn, $fileOut, $noItems=1)
> {
> $reRss=file_get_contents($fileIn);
> $ary=spliti('<item>', $reRss);
> $limit=$noItems;
> $reRss='';
> $i=0;
> while($i < $noItems)
> {
> $reRss.=$ary[$i];
> ++$i;
> }
> $reRss.='</channel></rss>';
> file_put_contents($fileOut, $reRss);
> )
>
>

Awesome, thanks...I'll try it when I get home later.
zach [ Do, 26 April 2007 14:14 ] [ ID #1698429 ]

Re: PHP RSS SCRIPT

"zach" <wackzingo [at] gmail.com> wrote in message
news:vJKdnVn6DcAwCq3bnZ2dnUVZ_ubinZ2d [at] wavecable.com...
> Vince Morgan wrote:
> > "Vince Morgan" <vinhar [at] REMOVEoptusnet.com.au> wrote in message
> > news:463070bb$0$5743$afc38c87 [at] news.optusnet.com.au...
> >> "zach" <wackzingo [at] gmail.com> wrote in message
> >> news:F7SdnewfZeP1na3bnZ2dnUVZ_gCdnZ2d [at] wavecable.com...
> >>> Hello everyone,
> >>>
> >>> I was looking for short simple script to show rss feeds on my website
> >>> and I found this one and it works awesome except I don't know what to
> >>> add or modify to limit the number of entries it shows. The entire
script
> >>> is posted below. Thanks in advance for the help.
> >>>
> > Too hurried sorry. Made an error
> >
> >> funcion chopRss($fileIn, $fileOut, $noItems)
> > First param is the name of the rss file you want to chop, second is the
file
> > to write the results to, and the last is the number of items you want.
> >
> > function chopRss($fileIn, $fileOut, $noItems=1)
> > {
> > $reRss=file_get_contents($fileIn);
> > $ary=spliti('<item>', $reRss);
> > $limit=$noItems;
> > $reRss='';
> > $i=0;
> > while($i < $noItems)
> > {
> > $reRss.=$ary[$i];
> > ++$i;
> > }
> > $reRss.='</channel></rss>';
> > file_put_contents($fileOut, $reRss);
> > )
> >
> >
>
> Awesome, thanks...I'll try it when I get home later.
There is some room for failure in the above code Zach. If the number of
items you want is greater than the number actualy in the original file you
will get an "undefined index" error.. It doesn't check that the file path
is valid, and it will overwrite the destination file without warning.
$limit is an artifact that I forgot to eliminate when I wrapped the code
into a function.
In other words, there is a little work for you to do too ;)
Vince Morgan [ Do, 26 April 2007 14:30 ] [ ID #1698430 ]
PHP » alt.php » PHP RSS SCRIPT

Vorheriges Thema: please help with cms for flash
Nächstes Thema: Some difficulty with file_exists