Jump to content

Help with RSS output?


mark s

Recommended Posts

Help with RSS output?

Hi i have a script to take a RSS Feed and convert it to a HTML Table output.
Ive modified the original code to output as a Table instead of a HTML Page.

It works fine (Please ignore the poor code, as i havent tidyed it up yet echo & print).

Now the problem comes about when i try to introduce a second feed into
my page, showing x2 feeds.
The first one will work but the second one produces the following error.

[!--coloro:#000099--][span style=\"color:#000099\"][!--/coloro--]Fatal error: Cannot redeclare startelement() (previously declared [!--colorc--][/span][!--/colorc--]

It there a way i can get the code to work with several feed.

I call the Feeds via a include file. (inc)

The Code that works with 1 Feed.

[code]

I cant post my code ?
I get the following error when i try?

---You don't have permission to access /forums/index.php on this server.
--Apache/1.3.33 Server at www.phpfreaks.com Port 80
It is wrapped in the code tags.
It contains php, functions...

If i can solve that i can post the code cheers.
But i can post with out it..
Should i comment //out all the code?
[/code]

I have tried include once with out success.

The result i'm after is to recreate many tables from diffrent RSS feeds,
on one page. I would need to use the include file to enable me to
position the output tables in there respective places.

Any help would be appreciated Thanks.....
Link to comment
Share on other sites

The " Fatal error: Cannot redeclare startelement() " is simple enough - your declaring the same function twice, or at least the same function name which PHP's namespacing abilities DO NOT allow.

To fix that error you would have to first, rename one of the functions which WILL impact anything that references that function. So there is more to it than just renaming the function.

As far as the error that the feed returns, that could be the feed host not allowing concurrent connections OR it could be that since your second function isn't firing that there is something not being processed by the second function that the feed is looking for.

I would fix the function issue first, then tackle the feed issue.
Link to comment
Share on other sites

Thanks i renamed the functions by simply adding a '2'
and it works.

Is it possible to do it in such away that the declared functions are reset / wiped
and allowed to run again?

or be renamed with a vaiable or somthing?

I could not post the code due to this line, after line after line of trying.
I've added - + - which should be removed for these forums to accept it.

[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]if (!($fp = fo- + -pen($file, "r"))) {[!--colorc--][/span][!--/colorc--]

So i can show the code now,
any sugestions would be welcome, how i can prevent the functions clashing,
with out manually renaming evey inc file.

[code]
The Code that works with 1 Feed.
Its not pretty but works - will tidy when finished.
=================================================================
[code]
$color1 = "#252D8B";
$color2 = "#2B34A1";
$row_count = 0;
/*  PHP RSS Reader v1.1
    By Richard James Kendall
    Bugs to richard@richardjameskendall.com
    Free to use, please acknowledge me
    
    Place the URL of an RSS feed in the $file variable.
       
       The $rss_channel array will be filled with data from the feed,
       every RSS feed is different by by and large it should contain:
       
       Array {
           [TITLE] = feed title
           [DESCRIPTION] = feed description
           [LINK] = link to their website
           
           [IMAGE] = Array {
                       [URL] = url of image
                       [DESCRIPTION] = alt text of image
                   }
           
           [ITEMS] = Array {
                       [0] = Array {
                               [TITLE] = item title
                               [DESCRIPTION] = item description
                               [LINK = a link to the story
                           }
                       .
                       .
                       .
                   }
       }
       
       By default it retrives the Reuters Oddly Enough RSS feed. The data is put into the array
       structure so you can format the information as you see fit.
*/
set_time_limit(0);


$file = "http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml";

$rss_channel = array();
$currently_writing = "";
$main = "";
$item_counter = 0;


function startElement($parser, $name, $attrs) {
       global $rss_channel, $currently_writing, $main;
       switch($name) {
           case "RSS":
           case "RDF:RDF":
           case "ITEMS":
               $currently_writing = "";
               break;
           case "CHANNEL":
               $main = "CHANNEL";
               break;
           case "IMAGE":
               $main = "IMAGE";
               $rss_channel["IMAGE"] = array();
               break;
           case "ITEM":
               $main = "ITEMS";
               break;
           default:
               $currently_writing = $name;
               break;
       }
}

function endElement($parser, $name) {
       global $rss_channel, $currently_writing, $item_counter;
       $currently_writing = "";
       if ($name == "ITEM") {
           $item_counter++;
       }
}

function characterData($parser, $data) {
    global $rss_channel, $currently_writing, $main, $item_counter;
    if ($currently_writing != "") {
        switch($main) {
            case "CHANNEL":
                if (isset($rss_channel[$currently_writing])) {
                    $rss_channel[$currently_writing] .= $data;
                } else {
                    $rss_channel[$currently_writing] = $data;
                }
                break;
            case "IMAGE":
                if (isset($rss_channel[$main][$currently_writing])) {
                    $rss_channel[$main][$currently_writing] .= $data;
                } else {
                    $rss_channel[$main][$currently_writing] = $data;
                }
                break;
            case "ITEMS":
                if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
                    $rss_channel[$main][$item_counter][$currently_writing] .= $data;
                } else {
                    //print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>");
                    $rss_channel[$main][$item_counter][$currently_writing] = $data;
                }
                break;
        }
    }
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fo- + -pen($file, "r"))) {
    die("could not open XML 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);

// output as HTML
//print ("<html><head><title>PHP RSS Reader</title></head><body>");
//if (isset($rss_channel["IMAGE"])) {
//    print ("<a href=\"" . $rss_channel["LINK"] . "\" target=\"_blank\"><img border=\"0\" src=\"" . $rss_channel["IMAGE"]["URL"] . "\" align=\"middle\" alt=\"" . $rss_channel["IMAGE"]["TITLE"] . "\"></a>  <font size=\"5\">" . $rss_channel["TITLE"] . "</font><br><br>");
//} else {
//    print ("<font size=\"5\">" . $rss_channel["TITLE"] . "</font><br><br>");
//}

echo "<table align='left' width='150' border='1' cellpadding='0' cellspacing='0' bordercolor='#252D8B' bgcolor='#252D8B'>";
echo "<tr><td bgcolor='#252D8B'><i>$rss_channel[DESCRIPTION]</i></td></tr>";

if (isset($rss_channel["ITEMS"])) {
    if (count($rss_channel["ITEMS"]) > 0) {
        for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) {
        $row_color = ($row_count % 2) ? $color1 : $color2;
        //echo "<tr><td bgcolor='$row_color' align = 'Right'>$row[2]</td></tr>";
            //print ("\n<table width=\"100%\" border=\"1\"><tr><td width=\"100%\"><a href=\"" . $rss_channel["ITEMS"][$i]["LINK"] . "\" target=\"_blank\"><h2>" . $rss_channel["ITEMS"][$i]["TITLE"] . "</h2></a></b>");

            print ("<tr><td width=\"100%\" bgcolor='$row_color'><a href=\"" . $rss_channel["ITEMS"][$i]["LINK"] . "\" target=\"_blank\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a>");
                        print ("</td></tr>");
            $row_count++;
        }
    } else {
        print ("<b>There are no articles in this feed.</b>");
    }
}

echo  "</table>";



[/code]
The above is in a inc file and works fine.

The result i'm after is to recreate many tables from diffrent RSS feeds,
on one page. I would need to use the include file to enable me to
position the output tables in there respective places.

Can the function names that are causing the re declare error
be changed on the fly so to speak.

Where i dont have to rename every function manually in each file?
But change there name by ++ count or somthing.

I'm not being lazy, just curious if that method is at all possible,
as it would be handy for future refrence.

Thanks in advance mark.

Any help would be appreciated Thanks.....
Link to comment
Share on other sites

You have to put the part of the code that calls the parse functions into a function itself, then call it and have it return your html...

[code]
<?php

$color1 = "#252D8B";
$color2 = "#2B34A1";
$row_count = 0;
/*  PHP RSS Reader v1.1
    By Richard James Kendall
    Bugs to richard@richardjameskendall.com
    Free to use, please acknowledge me
    
    Place the URL of an RSS feed in the $file variable.
      
       The $rss_channel array will be filled with data from the feed,
       every RSS feed is different by by and large it should contain:
      
       Array {
           [TITLE] = feed title
           [DESCRIPTION] = feed description
           [LINK] = link to their website
          
           [IMAGE] = Array {
                       [URL] = url of image
                       [DESCRIPTION] = alt text of image
                   }
          
           [ITEMS] = Array {
                       [0] = Array {
                               [TITLE] = item title
                               [DESCRIPTION] = item description
                               [LINK = a link to the story
                           }
                       .
                       .
                       .
                   }
       }
      
       By default it retrives the Reuters Oddly Enough RSS feed. The data is put into the array
       structure so you can format the information as you see fit.
*/
set_time_limit(0);

$rss_channel = array();
$currently_writing = "";
$main = "";
$item_counter = 0;

function startElement($parser, $name, $attrs) {
       global $rss_channel, $currently_writing, $main;
       switch($name) {
           case "RSS":
           case "RDF:RDF":
           case "ITEMS":
               $currently_writing = "";
               break;
           case "CHANNEL":
               $main = "CHANNEL";
               break;
           case "IMAGE":
               $main = "IMAGE";
               $rss_channel["IMAGE"] = array();
               break;
           case "ITEM":
               $main = "ITEMS";
               break;
           default:
               $currently_writing = $name;
               break;
       }
}

function endElement($parser, $name) {
       global $rss_channel, $currently_writing, $item_counter;
       $currently_writing = "";
       if ($name == "ITEM") {
           $item_counter++;
       }
}

function characterData($parser, $data) {
    global $rss_channel, $currently_writing, $main, $item_counter;
    if ($currently_writing != "") {
        switch($main) {
            case "CHANNEL":
                if (isset($rss_channel[$currently_writing])) {
                    $rss_channel[$currently_writing] .= $data;
                } else {
                    $rss_channel[$currently_writing] = $data;
                }
                break;
            case "IMAGE":
                if (isset($rss_channel[$main][$currently_writing])) {
                    $rss_channel[$main][$currently_writing] .= $data;
                } else {
                    $rss_channel[$main][$currently_writing] = $data;
                }
                break;
            case "ITEMS":
                if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
                    $rss_channel[$main][$item_counter][$currently_writing] .= $data;
                } else {
                    //print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>");
                    $rss_channel[$main][$item_counter][$currently_writing] = $data;
                }
                break;
        }
    }
}

function getrsshtml($feedurl) {
    global $rss_channel;
    global $currently_writing;
    global $main;
    global $item_counter;
    
    $rss_channel = array();
    $currently_writing = "";
    $main = "";
    $item_counter = 0;

    $file = $feedurl;
    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    if (!($fp = f open($file, "r"))) {
        return "could not open XML input";
    }
    
    while ($data = fread($fp, 4096)) {
        if (!xml_parse($xml_parser, $data, feof($fp))) {
            return (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);
    
    $html = "<table align='left' width='150' border='1' cellpadding='0' cellspacing='0' bordercolor='#252D8B' bgcolor='#252D8B'>";
    $html .= "<tr><td bgcolor='#252D8B'><i>$rss_channel[DESCRIPTION]</i></td></tr>";
    
    if (isset($rss_channel["ITEMS"])) {
        if (count($rss_channel["ITEMS"]) > 0) {
            for($i = 0;$i < count($rss_channel["ITEMS"]);$i++) {
            $row_color = ($row_count % 2) ? $color1 : $color2;
                $html .= "<tr><td width=\"100%\" bgcolor='$row_color'><a href=\"" . $rss_channel["ITEMS"][$i]["LINK"] . "\" target=\"_blank\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a>";
                $html .= "</td></tr>";
                $row_count++;
            }
        } else {
            $html .= "<b>There are no articles in this feed.</b>";
        }
    }
    
    $html .= "</table>";
    
    return $html;
}

echo getrsshtml("http://rss.cnn.com/rss/cnn_topstories.rss");

echo getrsshtml("http://rss.cnn.com/rss/cnn_tech.rss");

[/code]

You can use 1000000000 rss feeds with that and never have a problem.
Link to comment
Share on other sites

Thanks,
will give that a go in the morning and report back.

Ive only just got into these RSS Feeds and i love them,
it think there great...

Yep ive got my phpfreaks RSS :)

---------------------------------------------

Thanks for the reply - will update with results :)
Link to comment
Share on other sites

That works great :)

Thanks for that.....

Its another diffrent way of showing RSS Feeds.

I will have to tweak it a little to display it the way i want,
but it does the job, looking at the feeds from one file,
which is better than indivdual files all over the place...

I also need to work out a way i can cap the max feed from any one feed.

Say 15 so i can format my page so it looks neat..

----------------------------------
So looking at the code it checks the length of the feed and creates an array to suit.

How can i write an if statment to say
dont show any more than 15 from a Rss feed?

as i'm wondering if the Feed count is less than 15 say only 7
would an if statment then fail ??

Any help would be appreciated thanks...

Mark...

Thanks for the code above its really neat, i like it.
Link to comment
Share on other sites

A problem i didnt expect has cropped up?

Ive continued to use the code in post 2 above, for formating purposed.
And i have re-named the functions with 2, 3, 4, and so on in respective inc files.
So i now have 4 RSS Feeds being displayed.
But one of them failed (For an hour or so).
But the knock on effect is that only half of my web page was displayed.
And any inc files after that one failed to show at all.
Like the code and all following code had been stopped.

This is the function that calls the Feed.
[code]
function characterData2($parser, $data) {
    global $rss_channel, $currently_writing, $main, $item_counter;
    if ($currently_writing != "") {
        switch($main) {
            case "CHANNEL":
                if (isset($rss_channel[$currently_writing])) {
                    $rss_channel[$currently_writing] .= $data;
                } else {
                    $rss_channel[$currently_writing] = $data;
                }
                break;
            case "IMAGE":
                if (isset($rss_channel[$main][$currently_writing])) {
                    $rss_channel[$main][$currently_writing] .= $data;
                } else {
                    $rss_channel[$main][$currently_writing] = $data;
                }
                break;
            case "ITEMS":
                if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
                    $rss_channel[$main][$item_counter][$currently_writing] .= $data;
                } else {
                    //print ("rss_channel[$main][$item_counter][$currently_writing] = $data<br>");
                    $rss_channel[$main][$item_counter][$currently_writing] = $data;
                }
                break;
        }
    }
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = f**open($file, "r"))) {
    die("could not open XML 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)));
    }
}
[/code]

And its this part i belive i need to modify..
To prevent the following error : could not open XML input
[code]
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = f**open($file, "r"))) {
    die("could not open XML 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)));
    }
[/code]

I read that replacing "Die" With "Return Would / could Help.
[code]
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = f**open($file, "r"))) {
    //die("could not open XML input");
    echo "Feed Not Available";
    return;
}

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)));
    }
[/code]

But it then caused an error from this part.
XML error..: s at line 1
[code]
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)));
    }
[/code]

I guess i looking for a way to prevent a failed Feed stopping the
rest of my code running.
Is it possible to make a timeout for recieving a Feed,
as opening my page can on occasion take what seems for ever? (20 seconds).

-----
Summary
Feeds are called from my main page as inc files.
There are 4 feeds 1,2,3,4 (inc files)
If feed 1 fails then everything stops?
Main page is only half loaded, and no other feeds are excecuted.
How can i keep the main page and none failing feeds to be shown.
I cant move my inc request on my main page due to formating.
And finally is there any way to time out a feed, to speed up the displaying of
my web page if a feed is not available.

Any help will be appreciated thanks.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.