Setting up rss feeds

Discussion in 'Coding Help' started by Big D, Dec 20, 2007.

  1. Big D

    Big D New Member

    Hello,

    I've been trying to figure out the basics of setting up a simple rss feed from, say, the BBC, on a .php page, but having gone through umpteen articles on the net I am going round in circles.

    I cannot figure out what I am meant to be creating file-wise to upload to site - and where - and what actually goes in the particular page I want the feed to appear on.

    If anyone can help me along the way with this one I'd be very grateful.

    Big D.
     
  2. Forbairt

    Forbairt Portfolio Building

  3. Forbairt

    Forbairt Portfolio Building

    should add
    Code:
    require_once 'rss_fetch.inc';
    
    $url = 'http://magpie.sf.net/samples/imc.1-0.rdf';
    $rss = fetch_rss($url);
    
    echo "Site: ", $rss->channel['title'], "<br>
    ";
    foreach ($rss->items as $item ) {
    	$title = $item[title];
    	$url   = $item[link];
    	echo "<a href=$url>$title</a></li><br>
    ";
    }
    
    
    You may also want to download the rss feed to your site and refresh it on intervals of 5 minutes 10 minutes so that you don't end up hammering the bbc website

    for example if you site gets hit once a minute ... that'd be 60 hits to the BBC ... if you site gets hit 10 times a minute .. it'd be 600 hits .. and so on ... so the bbc may not like you doing that

    You'll also be required to link back to their site / articles and give your source in most cases
     
  4. Big D

    Big D New Member

    MagpieRSS

    Forbairt, thank you for your reply.

    I've just posted a message in the General chat section to explain that I've had to learn on the hoof since I lost my webmaster of 10 years, so there are still gaping holes in my knowledge.

    However, it looks as though Magpierss has already been placed on my site for a feed set up by my former webmaster.

    I notice there are 4 rss files in the directory:

    rss.php
    rss_cache.inc
    rss_fetch.inc
    rss_parse.inc

    and the rss.php file reads as follows:

    <?php
    require_once 'rss_fetch.inc';
    $url = 'http://news.bbc.co.uk/rss/newsonline_uk_edition/northern_ireland/rss091.xml';
    $rss = fetch_rss($url);
    $id=0;
    echo "Site: ", $rss->channel['title'], "<br>\n";
    foreach ($rss->items as $item ) {
    if($id=="5") {
    echo "</td><td><p>\n";
    }
    if($id=="10") {
    echo "</td><td><p>\n";
    }
    if($id=="15") {
    break;
    }
    $id++;
    $title = $item[title];
    $url = $item[link];
    $description = $item[description];
    echo "<a href=$url>$title</a><br>$description</li><br>\n";
    }
    ?>

    What I can't figure out is what code I should put in the page on which I would like the feed to appear, i.e. how do I activate the feed on a given web page?
     
  5. Forbairt

    Forbairt Portfolio Building

    create a new file just to test its all working "filename.php"

    put all your code into it I've simplified it slightly ... I think this code was being included as part of another file.


    Code:
    <?php
    
    require_once 'rss_fetch.inc';
    
    $url = 'http://news.bbc.co.uk/rss/newsonline_uk_edition/northern_ireland/rss091.xml';
    $rss = fetch_rss($url);
    
    echo "Site: ", $rss->channel['title'], "<br>\n";
    
    foreach ($rss->items as $item ) {
    
     $title = $item[title];
     $url   = $item[link];
     $description   = $item[description];
     echo "<a href=$url>$title</a><br>$description</li><br>\n";
    
    }
    
    ?>
    
    once you know this is working ...

    anywhere you want this to appear you could just put the code
    <?PHP include "filename.php"; ?>
     
  6. Big D

    Big D New Member

    Well done!

    Forbairt, thank you very much for that - I have it working!

    Just a couple of quickies:

    1. Can these feeds be styled with css?
    2. Presumably the feed source can simply be changed by changing the URL?
     
  7. Forbairt

    Forbairt Portfolio Building

    Code:
    echo "<a href=$url>$title</a><br>$description</li><br>\n";
    
    there you have your link urls so yes you just have to give them a class or put the whole thing in a container div ? ... for example

    Code:
    <div class="rssFeed">
    ...
    ...
    </div>
    
    then its just down to you to style the output



    I'd almost recommend you remove the $url bit from your include file

    then when ever you want to include a rss feed
    Code:
    <?PHP
    $url = "http://www.myurlformyrssfeed.com/my.rss";
    include "filename.php"
    ?>
    
     
  8. Big D

    Big D New Member

    One very last question in meantime

    Right, I've managed to get the thing going and have styled it a bit with css, but the feed is pretty long - what way - if any - is there of controlling the number of items in the feed?

    Until I get familiar with what each piece of the code does and get more adept at rss feeds generally I will almost certainly require more help, but I have to say that I do really appreciate your helpfulness.

    To show my good faith in the meantime would a link from my website Library Ireland: Irish History and Culture to your site be of any use?

    Big D.
     
  9. Forbairt

    Forbairt Portfolio Building

    to control the number of items you could do something like this

    Code:
    [FONT=monospace]
    [/FONT]$rss = fetch_rss($url);
    $items = array_slice($rss->items, 0, 5);
    
    where 5 is ... the number of items...


    A link wouldn't hurt but isn't necessary :)
     
  10. Big D

    Big D New Member

    Number of items unaffected

    Forbairt,

    The array slice line hasn't affected the length of the feed - don't know whether I've done something wrong?--

    Code:
    <?php
    require_once 'rss_fetch.inc';
    $url = 'http://news.bbc.co.uk/rss/newsonline_uk_edition/northern_ireland/rss091.xml';
    $rss = fetch_rss($url);
    $items = array_slice($rss->items, 0, 5);
    echo "Site: ", $rss->channel['title'], "<br>\n";
    foreach ($rss->items as $item ) {
     $title = $item[title];
     $url   = $item[link];
     $description   = $item[description];
     echo "<a href=$url>$title</a><br>$description</li><br>\n";
    }
    ?>
    For what it might be worth I have set up a link to your site at the following url Free Irish Reference Works
     
  11. Forbairt

    Forbairt Portfolio Building

    cheers for the link :)


    Code:
    <?php
    require_once 'rss_fetch.inc';
    $url = 'http://news.bbc.co.uk/rss/newsonline_uk_edition/northern_ireland/rss091.xml';
    $rss = fetch_rss($url);
    $items = array_slice($rss->items, 0, 5);
    echo "Site: ", $rss->channel['title'], "<br>\n";
    foreach ($items as $item ) {
    $title = $item[title];
    $url = $item[link];
    $description = $item[description];
    echo "<a href=$url>$title</a><br>$description</li><br>\n";
    }
    ?>
    
    What we did was chop the 5 entries out into an array called $items

    As such we have to change our foreach statement

    enjoy :)
     
  12. Big D

    Big D New Member

    Merci beaucoup!

    Forbairt, you're a wizard - thank you SO much!
     
  13. Forbairt

    Forbairt Portfolio Building

    no problem ... :D

    all part of the service here at Irish Webmaster Forum :D
     
  14. ghost

    ghost New Member

    Any explanation for this behavior

    Any explanation for this behavior
    the following feed is from RTE
    RTE Sport News
    the two I am curious about
    1 St Vincent's 2-08 Tyrrellspass 0-07
    2 O?Connor appointed to Kerins O?Rahillys post

    Source code for no 2
    <h1>O'Connor appointed to Kerins O'Rahillys post</h1>
    question why in the second case was the apostrophe converted to ?
     
  15. Big D

    Big D New Member

    No idea

    I wish I could help on this one, but it is beyond me - the apostrophes seem to be working in some items but not in others -strange.

    Hopefully someone with a bit more savvy can provide an answer
     
  16. Forbairt

    Forbairt Portfolio Building

    At a glance ... I'd say you've not defined your encoding for the page ?
     
  17. ghost

    ghost New Member

    And from the Irish Examiner sport

    And from the Irish Examiner sport
    Examiner Sport News
    Quotation marks are displayed as ?
    GAA due to ?serious? challenges to its amateur status.
     
  18. Forbairt

    Forbairt Portfolio Building

    can you post your php code ?


    This ...
    Code:
    <?php
    require_once 'rss_fetch.inc';
    
    $url = 'http://www.rte.ie/rss/sport.xml';
    $rss = fetch_rss($url);
    $items = array_slice($rss->items, 0, 5);
    echo "Site: ", $rss->channel['title'], "<br>\n";
    foreach ($items as $item ) {
    $title = $item[title];
    $url = $item[link];
    $description = $item[description];
    echo "<a href=$url>$title</a><br>$description</li><br>\n";
    }
    ?>
    
    works fine for me ... everything displaying correctly
     
  19. ghost

    ghost New Member

    RSS Test

    Used the code you posted for Big D
    I just ran the code in my own server to test it.
    I have used Last Rss on a few sites and I get a lot of this type of stuff the quotations in the Examiner site just made me drop it altogether.
    I don't know if this is an encoding problem , as you can see here apostrophes are showing in some instances and not in others
    just curious to know why.
     
  20. ghost

    ghost New Member

    last Rss

    I have Last Rss working here just useing titles
    StColumba's hurling Club Galway
    might notice the header on the right for independent.ie/sport/ is missing had to remove due to @?^*.
    I might have a look at magpie tonight looks like a sub for me.
     

Share This Page