Forums

[resolved] Display RSS news feed (17 posts)

  1. Anonymous
    Unregistered
    Posted 8 years ago #

    Here's a little code I wrote up that will parse and display a RSS news feed nicely. You can see it in action here: http://www.blog.jydesign.biz Note the stuff on the right about latest funny stuff. That's a RSS new feed.
    Here's the code. Add into index.php Not sure how to use myhacks.php yet, anyone with any pointers will be greatly apprecipated.
    <li id="news">Latest News

      <?php
      // Global variables for function use.
      $GLOBALS['title'] = false;
      $GLOBALS['link'] = false;
      $GLOBALS['description'] = false;
      $GLOBALS['titletext'] = null;
      $GLOBALS['linktext'] = null;
      $GLOBALS['desctext'] = null;
      $GLOBALS['counter'] = 0;
      // function: startElement
      // Deals with the starting element
      function startElement( $parser, $tagName, $attrs ) {
      // By setting global variable of tag name
      // I can determine which tag I am currently
      // parsing.
      switch( $tagName ) {
      case 'TITLE':
      $GLOBALS['title'] = true;
      break;
      case 'LINK':
      $GLOBALS['link'] = true;
      break;
      case 'DESCRIPTION':
      $GLOBALS['description'] = true;
      break;
      }
      }
      // function: endElement
      // Deals with the ending element
      function endElement( $parser, $tagName ) {
      // By noticing the closing tag,
      // I can print out the data that I want.
      switch( $tagName ) {
      case 'TITLE':
      $GLOBALS['counter']++;
      if ($GLOBALS['counter'] > 2) {
      echo "<br>";
      }
      $GLOBALS['title'] = false;
      //$GLOBALS['titletext'] = "";
      break;
      case 'LINK':
      if ($GLOBALS['counter'] > 2) {
      echo "" . $GLOBALS['titletext'][$GLOBALS['counter'] - 1] . "<br>";
      }
      $GLOBALS['link'] = false;
      $GLOBALS['linktext'] = "";
      break;
      /*case 'DESCRIPTION':
      echo "Desc: " . $GLOBALS['desctext'] . "";
      $GLOBALS['description'] = false;
      $GLOBALS['desctext'] = "";
      break;*/
      }
      }
      // function: charElement
      // Deals with the character elements (text)
      function charElement( $parser, $text ) {
      // Verify the tag that text belongs to.
      // I set the global tag name to true
      // when I am in that tag.
      if( $GLOBALS['title'] == true ) {
      $GLOBALS['titletext'][$GLOBALS['counter']] .= $text;
      } else if( $GLOBALS['link'] == true ) {
      $GLOBALS['linktext'] .= trim( $text );
      } else if( $GLOBALS['description'] == true ) {
      $GLOBALS['desctext'] .= htmlspecialchars( trim( $text ) );
      }
      }
      // Create an xml parser
      $xmlParser = xml_parser_create();
      // Set up element handler
      xml_set_element_handler( $xmlParser, "startElement", "endElement" );
      // Set up character handler
      xml_set_character_data_handler( $xmlParser, "charElement" );
      // Open connection to RSS XML file for parsing.
      $fp = fopen( "http://www.fun-emails.org/backend.php", "r" )
      or die( "Cannot read RSS data file." ); // Change the link to the link to your RSS feed.
      // Parse XML data from RSS file.
      while( $data = fread( $fp, 4096 ) ) {
      $data = str_replace("'", "'", $data);
      xml_parse( $xmlParser, $data, feof( $fp ) );
      }
      // Close file open handler
      fclose( $fp );
      // Free xml parser from memory
      xml_parser_free( $xmlParser );
      ?>
  • davidchait
    Member
    Posted 8 years ago #

    Yeah, if you want something a little easier to modify and configure, I posted my modified RSS aggregator code a while back. It's at:
    http://www.chait.net/index.php?p=85
    Feel free to take a look. It's flexible enough to adapt to new types of feeds pretty simply, as well as pulling out any particular subfields of the feed you might want.
    -d

  • WildDuck
    Member
    Posted 8 years ago #

    @ davidchait
    How do you get the newsfeed up and running. I cant find any readme in the zip file. Will u mind give me a short explanation.
    TIA.
    WildDuck

  • carthik
    Member
    Posted 8 years ago #

    @bitziz, you might want to post your hack and the instructions in your blog, or some page, and then link to it from the WP wiki so it does not get buried and forgotten in the forum.
    Someone else might find this useful too. :)

  • bitziz
    Member
    Posted 8 years ago #

    It is in my blog. Under WP Codes. The instruction is straight forward. I dunno, maybe if many people don't get it I will try and explain in more detail. As to WP wiki, I'm not sure how to add stuff to it so some pointers would be more than welcomed.

  • carthik
    Member
    Posted 8 years ago #

    Hi bitziz, using the Wiki is easy. You can sign in first using a name such as BitZiz with two capital letters in the same word.
    http://wiki.wordpress.org/index.php/HowToUseWiki
    that should give you the other instructions.
    Your hack goes in the myhacks wiki page, so just go to that page, click edit and copy and paste what others have done for their hacks on the same page.

  • bitziz
    Member
    Posted 8 years ago #

    Where do I signin? That's what I had trouble finding. Thanks for the help.

  • carthik
    Member
    Posted 8 years ago #

    Bottom right corner of the page, there is a text entry box called Sign In:
    If you are a new user, type in the new username thus - BitZiz and click sign in, or press return.
    np.

  • davidchait
    Member
    Posted 8 years ago #

    I've never been able to sign in to the Wiki, which is why I've never really made use of it. C'est la vie. ;)
    Wildduck:
    All you need to do is something like:
    $feedURL = "http://news.com.com/2547-1040_3-0-5.xml";
    $feed = parseURL($feedURL, 4, false, "feed-cnetttech", '', 36);
    The function signature is:
    function parseURL($InUrl, $numberOfItems, $showDetails, $cacheName, $filterCat='', $tLimit = -1, $dLimit = -1)
    $InUrl
    The url of the news feed.
    $numberOfItems
    How many items to display from the feed.
    $showDetails
    Show the title, or show the details/body as well?
    $cacheName
    What to name the cache file on disk (obv give it something unique from other feeds!)
    $filterCat
    Can leave as '' generally. Allows you to filter responses by matching the subitem urls, or subitem categories, with a particular string. For stripping down big feeds.
    $tLimit
    Character limit on titles. (truncates)
    $dLimit
    Character limit on descriptions. (truncates)
    I'm also going to update my files tonight, as I think some stuff is out of date in the zips.
    -d
    http://www.chait.net

  • davidchait
    Member
    Posted 8 years ago #

    Updated. Mini doc included and embedded in the main php file too.
    http://www.chait.net/index.php?p=85
    -d

  • bruce2004
    Member
    Posted 8 years ago #

    Well, tried the code here above, and feed links didn't work, tried code on your site bitziz, copied and pasted in index same, only all the code shows, tried the above download, and it's all greek to me.
    Miss "that other blog" now, easy to add feeds. Maybe I should stick to zfeeder and forget rss on WordPress, this dang php, if you don't know it can't do nuthin.
    But that's my fault I'm not a programmer, so can't understand half of this stuff

  • davidchait
    Member
    Posted 8 years ago #

    bruce -
    - How much have you modified your index.php?
    - Do you have a particular issue you are encountering?
    - What feed are you trying to display (in case there's a problem with it specifically)?
    Thanks!
    -d
    http://www.chait.net

  • bruce2004
    Member
    Posted 8 years ago #

    I copied and pasted the above code exactly. results here:
    http://www.bkdesign.ca/index.php
    I have 1.2 nightly build, but that shouldn't affect anything with this on sidebar (bottom)
    I'm just tired, downloaded and installed feedsplitter too, it said read install instructions, and there wasn't any, ergo, it didn't work either. Have about 12 feeds on my MT install lol, and have zfeeder as a stand alone that works good.
    Maybe I should do a php include of that

  • Anonymous
    Unregistered
    Posted 8 years ago #

    actually a phpinclude of zfeeder did work perfectly. Awesome, keeps this feed junkie happy

  • davidchait
    Member
    Posted 8 years ago #

    Bruce, would love to try and help you out. Just helped one other person get this up and running -- would like to continue to work out the kinks. And I'm looking toward adding an admin interface of some sort in the upcoming weeks, making it a bit easier to direct-include in a WP site as a sidebar or a 'subsite'.
    -d

  • bruce2004
    Member
    Posted 8 years ago #

    ok, will try it again

  • davidchait
    Member
    Posted 8 years ago #

    please do -- let me know where the issues are. I realize that having an admin interface would make things easier for the non-coder...
    -d

  • Topic Closed

    This topic has been closed to new replies.

    About this Topic

    Tags

    No tags yet.