1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Using an RSS Feed (This is Easy)

Discussion in 'XML & RSS' started by yfs1, Jan 5, 2005.

  1. #1
    Hello all, I wanted to be sure I always had fresh content on some of my sites so I started looking into RSS feeds but it was always way more complicated than it should have been.

    Well after a few weeks of experimentation, I have the code for two scenarios:

    1.) Add a straight RSS feed
    2.) Add a rotating RSS feed

    Just change the feed (it should be obvious where that is :) )

    You can get just about any feed you can think about here: www.syndic8.com

    For a Straight Feed

    <?php
    $insideitem = false;
    $tag = "";
    $title = "";
    $description = "";
    $link = "";
    function startElement($parser, $name, $attrs) {
     global $insideitem, $tag, $title, $description, $link;
     if ($insideitem) {
      $tag = $name;
     } elseif ($name == "ITEM") {
      $insideitem = true;
     }
    }
    function endElement($parser, $name) {
     global $insideitem, $tag, $title, $description, $link;
     if ($name == "ITEM") {
      printf("<dt><b><a href='%s'>%s</a></b></dt>",
      trim($link),htmlspecialchars(trim($title)));
      printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description)));
      $title = "";
      $description = "";
      $link = "";
      $insideitem = false;
     }
    }
    function characterData($parser, $data) {
     global $insideitem, $tag, $title, $description, $link;
     if ($insideitem) {
     switch ($tag) {
      case "TITLE":
      $title .= $data;
      break;
      case "DESCRIPTION":
      $description .= $data;
      break;
      case "LINK":
      $link .= $data;
      break;
     }
     }
    }
    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    $fp = fopen("http://michaelthompson.org/news/goo-world.xml","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);
    xml_parser_free($xml_parser);
    ?>
    Code (markup):
    For a rotating feed:

    <?php
    $insideitem = false;
    $tag = "";
    $title = "";
    $description = "";
    $link = "";
    $locations = array('http://michaelthompson.org/news/goo-world.xml', 'http://forums.seochat.com/external.php', 'http://michaelthompson.org/news/goo-world.xml');
    srand((float) microtime() * 10000000); // seed the random gen 
    $random_key = array_rand($locations);
    function startElement($parser, $name, $attrs) {
     global $insideitem, $tag, $title, $description, $link;
     if ($insideitem) {
      $tag = $name;
     } elseif ($name == "ITEM") {
      $insideitem = true;
     }
    }
    function endElement($parser, $name) {
     global $insideitem, $tag, $title, $description, $link;
     if ($name == "ITEM") {
      printf("<dt><b><a href='%s' target=new>%s</a></b></dt>",
      trim($link),htmlspecialchars(trim($title)));
      printf("<dt>%s</dt><br><br>",htmlspecialchars(trim($description)));
      $title = "";
      $description = "";
      $link = "";
      $insideitem = false;
     }
    }
    function characterData($parser, $data) {
     global $insideitem, $tag, $title, $description, $link;
     if ($insideitem) {
     switch ($tag) {
      case "TITLE":
      $title .= $data;
      break;
      case "DESCRIPTION":
      $description .= $data;
      break;
      case "LINK":
      $link .= $data;
      break;
     }
     }
    }
    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    $fp = fopen($locations[$random_key], '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);
    xml_parser_free($xml_parser);
    ?>
    Code (markup):
     
    yfs1, Jan 5, 2005 IP
    dirvish, DarrenC, onlinedude and 6 others like this.
  2. DangerMouse

    DangerMouse Peon

    Messages:
    275
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Cool, thanks for that... Does it work on all RSS versions?
     
    DangerMouse, Jan 5, 2005 IP
  3. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I haven't had a feed I couldn't use yet. If you want to post some feeds I will try them for you (I am a trial and error kind of person).

    You can check out the rotating feed on my directory:
    www.kwikgoblin.com

    It is around 2-3 weeks old and got a PR3.

    I believe however the rotating content has helped by encouraging Google to keep indexing. Google added 22 pages from day 7 to 14. Hopefully that keeps growing exponentially.

    I'm still experimenting with the feed code on that site (the format), so if it is down for 2-3 minutes it is because I am working on it.
     
    yfs1, Jan 5, 2005 IP
  4. TwisterMc

    TwisterMc Mac Guru

    Messages:
    972
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I just added my blog RSS feed to my homepage using Magpie RSS. Still needs some tweaks if I want descriptions but I have the titles and links at least. :)
     
    TwisterMc, Jan 5, 2005 IP
  5. ResaleBroker

    ResaleBroker Active Member

    Messages:
    1,665
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    90
    #5
    That's great. Thanks!
     
    ResaleBroker, Jan 5, 2005 IP
  6. Thewormman

    Thewormman Peon

    Messages:
    95
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Tried it works great!

    Thanks for sharing
     
    Thewormman, Jan 5, 2005 IP
  7. crafty

    crafty Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks for the informative post YSFI
    I am obviously doing something wrong as I tried both the scripts on a page of mine as a test without altering anything and they would not work.
    (Probably just a simple thing that I have missed)
    Any advice that you could offer would be most appreciated.
    Tony
     
    crafty, Jan 5, 2005 IP
  8. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #8
    First of all, what kind of page is it?

    If it is an html page, then be sure PHP is enabled.

    Put a piece of test PHP on your page and see if that works.

    I am answering this way because you called it a script which makes me think you are adding it into an html page although I could easily be wrong.

    Cheers
     
    yfs1, Jan 6, 2005 IP
  9. DangerMouse

    DangerMouse Peon

    Messages:
    275
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #9
    It could also be that you hosting provider (like mine :mad: ) is blocking outgoing port 80 connections on your web server... I had to ask them to open digitalpoint.com up on their fire wall before I got the co-op stuff running!
     
    DangerMouse, Jan 6, 2005 IP
  10. crafty

    crafty Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks..
    Yes I am attempting to add it to an html page.
    Hosting set up describes php as
    "PHP version 4.3.9"
    and I use php to redirect to affiliate URLs.
    if you can not think of a solution I will contact host as Dangermouse suggests.
    cheers
    Tony
     
    crafty, Jan 6, 2005 IP
  11. ZeRo_CoOl

    ZeRo_CoOl Peon

    Messages:
    635
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #11
    what is rss? if someone could provide a tut on using rss i would really appreciate it lots. thanks
     
    ZeRo_CoOl, Jan 6, 2005 IP
  12. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #12
    There isn't much to it. As it pertains to this thread, it allows you to put ever changing content on your page with no maintenance. Just choose a topic (there are millions) and there you go.

    I have an example at my directory.

    If you were to visit tommorow, the news stories would be different.
     
    yfs1, Jan 6, 2005 IP
  13. DangerMouse

    DangerMouse Peon

    Messages:
    275
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #13
    An RSS (Really Simple Syndication / RDF Site Summary depending on who you're speaking to) tutorial can be found here
     
    DangerMouse, Jan 7, 2005 IP
  14. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Thanks for that Danger...I will say though, I posted my original post because of a frustration of trying to decipher some of these tutorials. ost newbies eyes will glaze over after two lines of that so I just figured I would give a cut and paste solution.

    By the way, if you used my code please give me a rating (the little scales) with a comment. This will just let me know for the future if these type of things are usefull to people. In other words, if the feedback is good, I will post more like this.

    Thanks again Danger - By the way even the fact that RSS means Really Simple Syndication is a matter of debate.

    Cheers
     
    yfs1, Jan 7, 2005 IP
    eddie likes this.
  15. DangerMouse

    DangerMouse Peon

    Messages:
    275
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Let me tell you... It IS greatly appreciated!

    Unfortunately I can't use it though as my host doesn't allow me to use an outgoing port 80 connection (they say it's for security reasons - I've never heard of any!) unless you email them and asl... So I currenty use a little VB app to pull in the rss and build my pages on a local machine before I publish them - it's messy but does work + get you get the speed benefits of having it on a flat html page.

    Yeah - I heard a third def. today as well... for those wondering wtf I'm talking about. Here are the 3 definitions (I've heard) of RSS...

    1) Really Simple Syndication
    2) Rich Site Syndication
    3) RDF Site Syndication

    Anyway, it's a format / standard for syndicating content. Easy to read, easy to integrate. NICE ;)
     
    DangerMouse, Jan 7, 2005 IP
  16. Thewormman

    Thewormman Peon

    Messages:
    95
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #16
    yfs1

    I'm using your random script on a PHP dynamically generated page, and have found that if your script tries to load a feed where there is no output, at the time of the request, from the feed supplier, it just makes the whole page hang and not load.

    Any ideas how I can get round this?

    Many thanks
     
    Thewormman, Jan 7, 2005 IP
  17. miko67

    miko67 Well-Known Member

    Messages:
    769
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    120
    #17
    seems like a great "kiss" operation - thx a lot yfs1. Gonna try that real soon...

    oh, and by all means keep'em comming if you can. This is the stuff that good forums are made of.
     
    miko67, Jan 7, 2005 IP
  18. crafty

    crafty Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Thanks for all the information YSFI and Danger.
    I love the cut and paste idea, but still not happening for me unfortunately but am determined to get it up and running one way or another.
    (...I am in australia so you may have to print upside down for me to comprehend!!)
    Went crosseyed reading the php tutorial.
    My hosting details below may reveal something to you intelligent people :
    Host server is running php version 4.3.9, on Apache/1.3.33 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 PHP/4.3.9 FrontPage/5.0.2.2634a mod_ssl/2.8.22 OpenSSL/0.9.7a server software.

    Have asked host if port 80 problem may be it as suggested by D/M but no response as yet.
    Incidently, Danger, I note that you may be a horse racing fan. Great site down here is
    aapracingandsports.com.au/racing/index.asp
    Their Neural Algorythms (free) are very popular with people doing their own ratings.

    Thanks
    Tony
     
    crafty, Jan 7, 2005 IP
  19. mxlabs

    mxlabs Peon

    Messages:
    327
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #19
    I use rss feeds myself, works great.
    can you explain the difference between "straight" and "rotating" rss feed? do you rotate similar rss feeds from other sites to make the page look dynamic or do you simply update the rss feeds?
     
    mxlabs, Jan 7, 2005 IP
  20. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Those are just my words for lack of a better term.

    The straight feed is just one feed (as you will notice in the code) and is best suited when you want to be sure the visitor sees the same feed every time they come to that page.

    The rotating can incorporate a unlimited amount of feeds and just picks one of the many you designate each time. I am currently using this on my directory site in order for there to be "different content" on each subpage of my directory. When the bots visit each individual page it is never more than 10% alike. The only drawback to this is a visitor may see two things they like in a particular feed and when they try to go back to view the second, they won't be able to find it without reloading over and over.

    I am currently developing a solution to that however whcih I will post when done.

    Wormman - Can you PM me the feed that is causing the issue so I can recreate it?

    Cheers
     
    yfs1, Jan 8, 2005 IP