How do I Add content from an RSS or Atom feed to website

How do I Add content from an RSS or Atom feed to website

Page 1 of 1

7 Replies - 3670 Views - Last Post: 23 August 2011 - 07:14 AM

#1 Kweku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-March 09

How do I Add content from an RSS or Atom feed to website

Posted 30 September 2010 - 02:54 AM

Hi guys,

help please, How do I Add content from an RSS or Atom feed to website?

The website is in plain html, any help will be greatly appreciated.

Thanx in advance.
Is This A Good Question/Topic? 0
  • +

Replies To: How do I Add content from an RSS or Atom feed to website

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,025
  • Joined: 08-June 10

Re: How do I Add content from an RSS or Atom feed to website

Posted 30 September 2010 - 02:57 AM

with pure HTML/CSS that’s not possible, as RSS/Atom serves a totally different purpose. you would need a server side script that reads the XML, extracts the relevant information and prints it into your page. but as I said, RSS/Atom hardly contain anything worth putting in a website (maybe despite some links)
Was This Post Helpful? 0
  • +
  • -

#3 Kweku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-March 09

Re: How do I Add content from an RSS or Atom feed to website

Posted 30 September 2010 - 03:20 AM

What would you suggest I use? Would php not work? as I think the servers are linux servers not windows
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3479
  • View blog
  • Posts: 10,025
  • Joined: 08-June 10

Re: How do I Add content from an RSS or Atom feed to website

Posted 30 September 2010 - 03:39 AM

any XML capable server-side language (including PHP) would work.
Was This Post Helpful? 0
  • +
  • -

#5 Kweku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-March 09

Re: How do I Add content from an RSS or Atom feed to website

Posted 30 September 2010 - 11:53 AM

Any ideas how I can convert to show on the page? Links could also help if you have any.

Thanks man
Was This Post Helpful? 0
  • +
  • -

#6 Bagelwolf  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 12-April 09

Re: How do I Add content from an RSS or Atom feed to website

Posted 30 September 2010 - 02:57 PM

View PostKweku, on 30 September 2010 - 10:53 AM, said:

Any ideas how I can convert to show on the page?


You get the content to show on the page by using a server-side scripting language like PHP to access the feed data and load it into an array. Then you output the data in the array through a series of loops in your page.

Basically, you write one chunk of code as a template for each item in the array, so you set up a <div> or something that contains a place for the title, publishing date, description or content, etc. Then you enclose that in a PHP statement that tells the server to keep outputting that block of code as long as there are items in the array to display. So if you have 5 items coming in from the feed, it'll output that block of code 5 times - each time displaying the data from a different item in the array.

If you need help understanding PHP loops and data arrays and such, it should be easy to find help. This tutorial should give you a good start.
Was This Post Helpful? 0
  • +
  • -

#7 calebjonasson  Icon User is offline

  • $bert = new DragonUnicorn(); $bert->rawr();
  • member icon

Reputation: 209
  • View blog
  • Posts: 989
  • Joined: 28-February 09

Re: How do I Add content from an RSS or Atom feed to website

Posted 01 October 2010 - 08:33 AM

There is a free script that runs on php that does exactly what you want.

view.php
    <?php
   // Include the file that does all the work
   include("rssreader.php");
   $url = $_POST["rssinput"];

   // This is the URL to the actual RSS feed. Change this value
   // if you want to show a different feed.
   //$url="http://www.wired.com/news/feeds/rss2/0,2610,,00.xml";

   // Create an instance of the rssFeed object, passing it
   // the URL of the feed
   $rss=new rssFeed($url);

   // If there was an error getting the data
   if($rss->error){
      // Show the error
      print "<h1>Error:</h1>\n<p><strong>$rss->error</strong></p>";
   }else{

      // Otherwise, we have the data, so we call the parse method
      $rss->parse();

      // The showHeading can accept a paramater that will be used
      // as the tag to wrap the heading. In this case, we're wrapping
      // the title in an <h1> tag
      $rss->showHeading("h1");

      // Display the image if there is one
      $rss->showImage("left");

      // If the RSS feed provides a link
      if($rss->link){
         // Display it
         print "<p>Provided courtesy of:<br>\n";
         $rss->showLink();
      }

      // Display the description
      $rss->showDescription();

      // Show the news stories
      $rss->showStories();
   }
?>


rssreader.php
This document can be used but only if the author is credited, keep it in mind.

<?php
/*

File: rssreader.php
Author: Gary White
Last modified: May 2, 2005

June 21, 2005 - Added some minor error handling updates

May 2, 2005 - Modified again to drop using cURL library and
instead use direct socket i/o. This eliminates any external 
server configuration dependencies.

Apr 21, 2005 - Modified to use cURL library instead of 
file_get_contents function. This class now has a dependency
in that the curl library must be enabled on the server.

Copyright (C) 2005, Gary White

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details at:
http://www.gnu.org/copyleft/gpl.html

Typical usage:

    include("rssreader.php");
    $url="http://www.wired.com/news/feeds/rss2/0,2610,,00.xml";
    $rss=new rssFeed($url);
    if($rss->error){
        print "<h1>Error:</h1>\n<p><strong>$rss->error</strong></p>";
    }else{
        $rss->parse();
        $rss->showHeading("h1");
        if($rss->link){
            print "<p>Provided courtesy of:<br>\n";
            print "<a href=\"$rss->link\">$rss->link</a>\n";
        }
        $rss->image->show("middle");
        $rss->showDescription();
        $rss->showStories();
    }


rssFeed: object
    Purpose: Creates an rssFeed class that allows easy display of RSS data.
    
    Public Methods:
        parse           Parses the XML and populates properties with the resulting data
        showHeading     Displays the title
        showImage       Displays the image associated with the feed
        showLink        Displays a link to the RSS provider
        showDescription Displays the feed description
        showStories     Displays the feed items
    
    Public Properties:
        title:          The title of the RSS feed
        copyright:      The copyright information included in the RSS feed
        description:    The description included in the RSS feed
        image           An instance of an rssImage object (see below)
        stories         An array of newsStory objects (see below)
        url             The URI of the RSS feed
        xml             The raw XML data obtained from the feed
        error           A text description of the most recent error encountered
        maxstories      The maximum number of stories to show (zero = no limit)

rssImage: object
    Purpose: Creates a class to store information about an image for an RSS feed

    Public Methods:
        show            Displays the image

    Public Properties:
        title           The image title, used for ALT attribute
        url             The URL of the image
        link            The URL that the image should link to
        width           The image width in pixels
        height          The image height in pixels

newsStory: object
    Purpose: Creates a class to store an news story in an RSS feed

    Public Methods:
        show            Displays the story

    Public Properties:
        show            Displays the story
        title           The headline associated with the story
        link            The link to the full story
        description     A short description, or teaser, of the story
        pubdate         Date/Time the story was published
    
*/

//classes follow

// Generic container for the complete RSS feed
class rssFeed{
    var $title="";
    var $copyright="";
    var $description="";
    var $image;
    var $stories=array();
    var $url="";
    var $xml="";
    var $link="";
    var $error="";
    var $maxstories=0;
    
    // public methods
    function parse(){
        $parser=xml_parser_create();
        xml_set_element_handler($parser, "startElement", "endElement");
        xml_set_character_data_handler($parser, "characterData");
        xml_parse($parser, $this->xml, true)
            or die(sprintf("XML error: %s at line %d", 
                xml_error_string(xml_get_error_code($parser)), 
                xml_get_current_line_number($parser)));
        xml_parser_free($parser);
    }

    function showHeading($tag=""){
        $tag=$tag?$tag:"h1";
        if($this->title)
            print "<$tag>$this->title</$tag>\n";
    }

    function showImage($align=""){
        $this->image->show($align);
    }

    function showLink(){
        if($this->link)
            print "<a href=\"$this->link\">$this->link</a>\n";
    }    
    function showDescription(){
        if($this->description)
            print "<p>$this->description</p>\n";
    }

    function showStories(){
        echo "<dl>\n";
        $n=0;
        foreach($this->stories as $story){
            $n++;
            if ($this->maxstories && $n>$this->maxstories)
                break;
            $story->show();
        }
        echo "</dl>\n";
    }
    
    // Methods used internally
    // Constructor: Expects one string parameter that is the URI of the RSS feed
    function rssFeed($uri=''){
        $this->image=new rssImage();
        if($uri){
            $this->url=$uri;
            $this->getFeed();
        } else {
            $this->error="No URL for RSS feed";
        }
    }
    
    // Retrieves the XML from the RSS supplier
    function getFeed(){
        // if we have a URL
        if ($this->url){
            if (extension_loaded('curl')) {
                $this->xml=$this->getRemoteFile($this->url);
            }
        }
    }

    function getRemoteFile($url){
        $s=new gwSocket();
        if($s->getUrl($url)){
            if(is_array($s->headers)){
                $h=array_change_key_case($s->headers, CASE_LOWER);
                if($s->error) // failed to connect with host
                    $buffer=$this->errorReturn($s->error);
                elseif(preg_match("/404/",$h['status'])) // page not found
                    $buffer=$this->errorReturn("Page Not Found");
                elseif(preg_match("/xml/i",$h['content-type'])) // got XML back
                    $buffer=$s->page;
                else // got a page, but wrong content type
                    $buffer=$this->errorReturn("The server did not return XML. The content type returned was ".$h['content-type']);
            } else {
                $buffer=$this->errorReturn("An unknown error occurred.");
            }
        }else{
            $buffer=$this->errorReturn("An unknown error occurred.");
        }
        return $buffer;
    }

    function errorReturn($error){
        $retVal="<?xml version=\"1.0\" ?>\n".
            "<rss version=\"2.0\">\n".
            "\t<channel>\n".
            "\t\t<title>Failed to Get RSS Data</title>\n".
            "\t\t<description>An error was ecnountered attempting to get the RSS data: $error</description>\n".
            "\t\t<pubdate>".date("D, d F Y H:i:s T")."</pubdate>\n".
            "\t\t<lastbuilddate>".date("D, d F Y H:i:s T")."</lastbuilddate>\n".
            "\t</channel>\n".
            "</rss>\n";
        return $retVal;
    }

    function addStory($o){
        if(is_object($o))
            $this->stories[]=$o;
        else
            $this->error="Type mismatach: expected object";
    }

}

class rssImage{
    var $title="";
    var $url="";
    var $link="";
    var $width=0;
    var $height=0;
    
    function show($align=""){
        if($this->url){
            if($this->link)
                print "<a href=\"$this->link\">";
            print "<img src=\"$this->url\" style=\"border:none;\"";
            if($this->title)
                print " alt=\"$this->title\"";
            if($this->width)
                print " width=\"$this->width\" height=\"$this->height\"";
            if($align)
                print " align=\"$align\"";
            print ">";    
            if($this->link)
                print "</a>";
        }
    }
}

class newsStory{
    var $title="";
    var $link="";
    var $description="";
    var $pubdate="";
    
    function show(){
        if($this->title){
            if($this->link){
                echo "<dt><a href=\"$this->link\">$this->title</a></dt>\n";
            }elseif($this->title){
                echo "<dt>$this->title</a></dt>\n";
            }
            echo "<dd>";
            if($this->pubdate)
                echo "<i>$this->pubdate</i> - ";
            if($this->description)
                echo "$this->description";
            echo "</dd>\n";
        }
    }
}


class gwSocket{
    var $Name="gwSocket";
    var $Version="0.1";
    var $userAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
    var $headers;
    var $page="";
    var $result="";
    var $redirects=0;
    var $maxRedirects=3;
    var $error="";

    function getUrl( $url ) {
        $retVal="";
        $url_parsed = parse_url($url);
        $scheme = $url_parsed["scheme"];
        $host = $url_parsed["host"];
        $port = $url_parsed["port"]?$url_parsed["port"]:"80";
        $user = $url_parsed["user"];
        $pass = $url_parsed["pass"];
        $path = $url_parsed["path"]?$url_parsed["path"]:"/";
        $query = $url_parsed["query"];
        $anchor = $url_parsed["fragment"];

        if (!empty($host)){

            // attempt to open the socket
            if($fp = fsockopen($host, $port, $errno, $errstr, 2)){

                $path .= $query?"?$query":"";
                $path .= $anchor?"$anchor":"";

                // this is the request we send to the host
                $out = "GET $path ".
                    "HTTP/1.0\r\n".
                    "Host: $host\r\n".
                    "Connection: Close\r\n".
                    "User-Agent: $this->userAgent\r\n";
                if($user)
                    $out .= "Authorization: Basic ".
                        base64_encode("$user:$pass")."\r\n";
                $out .= "\r\n";

                fputs($fp, $out);
                while (!feof($fp)) {
                    $retVal.=fgets($fp, 128);
                }
                fclose($fp);
            } else {
                $this->error="Failed to make connection to host.";//$errstr;
            }
            $this->result=$retVal;
            $this->headers=$this->parseHeaders(trim(substr($retVal,0,strpos($retVal,"\r\n\r\n"))));
            $this->page=trim(stristr($retVal,"\r\n\r\n"))."\n";
            if(isset($this->headers['Location'])){
                $this->redirects++;
                if($this->redirects<$this->maxRedirects){
                    $location=$this->headers['Location'];
                    $this->headers=array();
                    $this->result="";
                    $this->page="";
                    $this->getUrl($location);
                }
            }
        }
        return (!$retVal="");
    }
    
    function parseHeaders($s){
        $h=preg_split("/[\r\n]/",$s);
        foreach($h as $i){
            $i=trim($i);
            if(strstr($i,":")){
                list($k,$v)=explode(":",$i);
                $hdr[$k]=substr(stristr($i,":"),2);
            }else{
                if(strlen($i)>3)
                    $hdr[]=$i;
            }
        }
        if(isset($hdr[0])){
            $hdr['Status']=$hdr[0];
            unset($hdr[0]);
        }
        return $hdr;
    }

}

/*
    end of classes - global functions follow
*/

function startElement($parser, $name, $attrs) {
    global $insideitem, $tag, $isimage;
    $tag = $name;
    if($name=="IMAGE")
        $isimage=true;
    if ($name == "ITEM") {
        $insideitem = true;
    }
}

function endElement($parser, $name) {
    global $insideitem, $title, $description, $link, $pubdate, $stories, $rss, $globaldata, $isimage;
    $globaldata=trim($globaldata);
    // if we're finishing a news item
    if ($name == "ITEM") {
        // create a new news story object
        $story=new newsStory();
        // assign the title, link, description and publication date
        $story->title=trim($title);
        $story->link=trim($link);
        $story->description=trim($description);
        $story->pubdate=trim($pubdate);
        // add it to our array of stories
        $rss->addStory($story);
        // reset our global variables
        $title = "";
        $description = "";
        $link = "";
        $pubdate = "";
        $insideitem = false;
    } else {
        switch($name){
            case "TITLE":
                if(!$isimage)
                    if(!$insideitem)
                        $rss->title=$globaldata;
                break;
            case "LINK":
                if(!$insideitem)
                    $rss->link=$globaldata;
                break;
            case "COPYRIGHT":
                if(!$insideitem)
                    $rss->copyright=$globaldata;
                break;
            case "DESCRIPTION":
                if(!$insideitem)
                    $rss->description=$globaldata;
                break;
        }
    }
    if($isimage){
        switch($name){
            case "TITLE": $rss->image->title=$globaldata;break;
            case "URL": $rss->image->url=$globaldata;break;
            case "LINK": $rss->image->link=$globaldata;break;
            case "WIDTH": $rss->image->width=$globaldata;break;
            case "HEIGHT": $rss->image->height=$globaldata;break;
        }
    }
    if($name=="IMAGE")    
        $isimage=false;
    $globaldata="";
}

function characterData($parser, $data) {
    global $insideitem, $tag, $title, $description, $link, $pubdate, $globaldata;
    if ($insideitem) {
        switch ($tag) {
            case "TITLE":
                $title .= $data;
                break;
            case "DESCRIPTION":
                $description .= $data;
                break;
            case "LINK":
                $link .= $data;
                break;
            case "PUBDATE":
            case "DC:DATE":
                $pubdate .= $data;
                break;
        }
    } else {
        $globaldata.=$data;
    }
}

?>

Was This Post Helpful? 0
  • +
  • -

#8 WebDi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-August 11

Re: How do I Add content from an RSS or Atom feed to website

Posted 23 August 2011 - 07:14 AM

If anyone is still monitoring this thread, I'd like to know how to determine (or shorten) the length of the amount of the article that is displayed. I mean specifically the "description".
Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1