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

    saintdw Peon

    Messages:
    453
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #61
    Let me play with it a little and ill post when it works
     
    saintdw, Feb 28, 2005 IP
  2. saintdw

    saintdw Peon

    Messages:
    453
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #62
    <?php
    $insideitem = false;
    $tag = "";
    $title = "";
    $description = "";
    $link = "";
    $locations = array('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.");
    $int = 0;
    while ($int != [b]n[/b]){
    [b]$data = fread($fp, 1024);[/b]
    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)));
    
    $int++;
    }
    fclose($fp);
    
    xml_parser_free($xml_parser);
    
    ?>
    Code (markup):
    n in while loop number to be changed.

    Here's a quick fix or something.

    n= 1 --- 1 item displayed
    n= 2 && fread($data, 256*3) --- 2 items displayed
    n= 2 --- 3 items displayed
    n= 3 --- 5 items displayed
    n= 4 --- 7 items displayed

    Not really a php expert, but to get the desired number of feeds you will needs to play around with 2 numbers:
    N and the number in fread just under N

    number in fread can be a multiple of 512, 1024... etc.

    Play around with them until the desired number of items is displayed
     
    saintdw, Feb 28, 2005 IP
  3. saintdw

    saintdw Peon

    Messages:
    453
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #63
    If you dont want the links you can further edit $data and remove <link></link> if you know php good enough and then just echo the data.
     
    saintdw, Feb 28, 2005 IP
  4. saintdw

    saintdw Peon

    Messages:
    453
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #64
    also, those numbers i tested only with the feed included in the code.

    If you use another feed you will probably have to pick new numbers to achieve the same amount of items displayed.
     
    saintdw, Feb 28, 2005 IP
  5. Thewormman

    Thewormman Peon

    Messages:
    95
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #65
    OK
    Nice to be able to contribute to this :D
    Made a small addition in BOLD that will add the new 'no follow' attribute to all the links to the news items.

    This means you will not pass your PR to people like CNN who really don't need it!
    <?php
    $insideitem = false;
    $tag = "";
    $title = "";
    $description = "";
    $link = "";
    $locations = array('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' [B]target='new' rel='nofollow'[/B]>%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.");
    $int = 0;
    while ($int != n){
    $data = fread($fp, 1024);
    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)));
    
    $int++;
    }
    fclose($fp);
    
    xml_parser_free($xml_parser);
    
    ?>
    Code (markup):
    Also I am getting this

    XML error: junk after document element at line 1 
    Code (markup):
    Turning up at the bottom of some of my feeds and it is slowing the page loading a LOT

    Anyone know how to get rid?

    Thanks
     
    Thewormman, Mar 2, 2005 IP
    yfs1 likes this.
  6. dirvish

    dirvish Peon

    Messages:
    142
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #66
    If you don't want to mess with any programming and want to go from an RSS feed to html I recommend RSS Digest: http://www.bigbold.com/rssdigest/

    If you just want to copy and paste some PHP, try out this script: http://www.feedforall.com/free-php-script.htm
     
    dirvish, Mar 3, 2005 IP
  7. yfs1

    yfs1 User Title Not Found

    Messages:
    13,798
    Likes Received:
    922
    Best Answers:
    0
    Trophy Points:
    0
    #67
    Why would they use your script and give you a link when there is a perfectly good one here with no strings attached??

    You are urging people to not use the one provided totally free here basically by calling it programming when you are giving a link to essentially the same code.
     
    yfs1, Mar 4, 2005 IP
  8. dirvish

    dirvish Peon

    Messages:
    142
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #68
    Didn't mean to offend and I wasn't looking for a link. Just providing a couple alternatives...
     
    dirvish, Mar 4, 2005 IP
  9. CanadianEh

    CanadianEh Notable Member

    Messages:
    3,812
    Likes Received:
    380
    Best Answers:
    0
    Trophy Points:
    260
    #69
    Thanks for the usefull addition. I get the same XML error. The error goes away when I decrease "n" sufficiently.

    saintdw
    I know next to nothing about PHP, but it looks like you get XML error whenever you try to display more items than there are available for the feed(e.g. n is too high). Your addition needs something to to exit the loop in order to complete it.
     
    CanadianEh, Mar 9, 2005 IP
  10. saintdw

    saintdw Peon

    Messages:
    453
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #70

    Cant say im a php expert my self.

    My addition is focused on limiting the number of items displayed havent tested it with large n numbers.

    If you want everything displayed then the original works great :)
     
    saintdw, Mar 9, 2005 IP
  11. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #71
    Does this code cache the feed or hit the feed everytime someone displays a page using this code?

    You should really read this... http://www.kbcafe.com/rss/rssfeedstate.html

    There are alot of guidlines to help keep your app from being a "problem" reader you may want to consider.
     
    noppid, Mar 9, 2005 IP
  12. CanadianEh

    CanadianEh Notable Member

    Messages:
    3,812
    Likes Received:
    380
    Best Answers:
    0
    Trophy Points:
    260
    #72
    Hi yfs1

    Thanks again for the script. Is there a way of displaying 2 separate feeds on the same page?
     
    CanadianEh, Mar 9, 2005 IP
  13. tfbpa

    tfbpa The....Alive

    Messages:
    896
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    120
    #73
    Exactly what I needed and was looking for, works like a charm!
    Thank You yfs1 !!
     
    tfbpa, Mar 20, 2005 IP
  14. Lorem Ipsum

    Lorem Ipsum Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #74
    I was able to get this started on our site as well - thanks for the code. For one feed I pulled, I got the following error:

    XML error: not well-formed (invalid token) at line 41

    This was the code from line 41:

    $link .= $data;
    PHP:

    This error did not show up with the original two feeds listed in this thread. Any idea how I might go about straightening this out? Could this be a problem on the publisher's end? (BTW, if there were a step before 'newbie' in php coding, I'd be there - so please go easy on me).


    Hmmm....discovered something else - presented on it's own page, it seems to work fine, other than the error noted above. But pasted into an existing php page, and it takes over, and no other content on the page is viewable. I'm certain that I'm goofing it up on my end, just not sure how to go about figuring out how. Any help is appreciated.


    And on a funny note - went to pull a feed from a certain site, and sure enough, the feed included an article I had written for another site. :)
     
    Lorem Ipsum, Mar 20, 2005 IP
  15. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #75
    Are you sure the error is on line 41 of your code? (As opposed to line 41 in an included class file?) Just a thought. I'm late to this thread.
     
    nevetS, Mar 20, 2005 IP
  16. Texacola

    Texacola Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #76
    Works very well! Thanks to all that contributed to this RSS thread, it's helped me loads. Now to find a darn feed that is even remotely related to inkjet cartridges, printers and toner that isn't a blatant advertisement of one of my opposition.

    edit* And it does break the heck out of html validation too! Would this cause any of the search engines to ignore the sites?

    I've added one to a couple of places to my site http://www.outofinkandtoner.com.au and would hate for Google (whenever the heck I get out of the sandbox) to penalise me for "bad code"!
     
    Texacola, Mar 21, 2005 IP
  17. Lorem Ipsum

    Lorem Ipsum Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #77
    nevetS - It is entirely possible that the error is not on my side. Unfortunately, I don't have the knowledge to troubleshoot the issue.
     
    Lorem Ipsum, Mar 21, 2005 IP
  18. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #78
    Usuallly, php will report the error:

    Error in file.php line 41.

    Keep in mind that line 41 to you isn't necessarily line 41 to php. Usually what I do when the error is not obvious, is copy my original file over to a backup, then try to strip some earlier code out of the equation so I can narrow down the problem. Another method is to put a couple of <? echo "I got here without a problem";?> lines. I believe that when php counts lines, it counts them after the includes - so if there are 50 lines in your first include, then it would be in line 41 of that file.

    I'm not very sure of myself right now (just woke up), but it's at least something to look into.
     
    nevetS, Mar 21, 2005 IP
  19. Padawan

    Padawan Peon

    Messages:
    1,085
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    0
    #79
    Say, for example, you are using a world news feed, is there any way to save the data each time the feed updates, so as to be able to build up an archive from day to day ?
     
    Padawan, Mar 22, 2005 IP
  20. Texacola

    Texacola Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #80
    Did you end up getting some sort of answer to this question Padawan as I'm extremely interested in the answer too?
     
    Texacola, Apr 3, 2005 IP