SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question parsing RSS feeds & displaying them in HTML

    Hi
    I am looking for some free script that can parse RSS feeds & display them using HTML formatting, just like the one at SitePoint. But I want it to parse RSS not RDF(which SitePoint uses). I know its easy but don't mock me, I'm not proficient in PHP yet, just taking a few tentative steps whenever I get some free time.

    At present, I tried using this script which a pal of mine gave to me.
    PHP Code:
    <?php
    $url 
    $_GET['feed'];
     
    // Get the URL to the RSS Feed
    $data=@fread(fopen("$url","r"),10000) or die("Cant open $url!");
     
    // Get the file contents
    $myar=getXmlData($data);
     
    // What ever it may be, the argument to this
     // function should be the XML document's content

    // Now $myar holds the fully parsed XML contents.
    // It may have a number of tags, like <TITLE> , <LINK> , <AUTHOR> , <SOMETHING> ... and so on
    // With a simple loop, you can extract all the tags and their values neatly
    // Remember, $i<= should be lesser than or equal to the number of values
    // a tag has. If you give count($myar), it will only take the number of tags.

    for($i=0;$i<=count($myar[TITLE]);$i++) {
    // Here, we want to read the TITLE, DESCRIPTION, and LINK of the RSS feed.
    $title=$myar[TITLE][$i];
    $text=$myar[DESCRIPTION][$i];
    $link=$myar[LINK][$i];
     if(
    $title) {
    echo <<<EOF
    <a href="$link"><font size="2" face="Verdana" color="blue">$title</font></a><br>
    <font size="2" face="Verdana">
    $text</font><br><br>
    <hr width="50%" size="1" align="left">
    EOF;
     }
    }
     
    //##############################################
    // This is the function. It returns the array of the parsed XML data
    function getXmlData($xml_doc) {
    $n=0;    // Counter used for arraying the XML data
    $ar=array(); // The main array for storing parsed xml using xml_parse_into_struct()
    // Parse the XML document
    $parser xml_parser_create();
    xml_parse_into_struct($parser,$xml_doc,$vals,$index) or die(xml_error_string(xml_get_error_code($parser)));
    xml_parser_free($parser);

    $ttags=array(); // Temporary arry for storing tag names

    // The main part.
    for($n=0;$n<=count($vals)-1;$n++) {
     if(
    trim($vals[$n][value])) {
     
    $ar[$vals[$n][tag]][count($ar[$vals[$n][tag]])]=$vals[$n][value];
     
    $ttags[$vals[$n][tag]]=$vals[$n][tag];
     }
    }

    // Array for storing all the tag names
    $tags=array();
    // Extract and save the tag names to the array
    foreach($ttags as $tagi) { array_push($tags,$tagi); }
    return 
    $ar;
    }
    ?>

    But when I try to pass it the url of an RSS feed at http://cyber.law.harvard.edu/blogs/g...mpleRss092.xml like
    http://localhost/feedReader.php?feed...mpleRss092.xml
    all it says that
    Quote Originally Posted by RSS Reader's Response
    unclosed token
    Now what does that mean & how do I get it working??
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Why not use XSLT to convert the RSS into HTML, and use PHP just to perform the transformation?

  3. #3
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    How??
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    This is a sample XSL stylesheet. It was written to convert RSS 1.0 to XHTML, but you should get an idea of how to do something similar:
    HTML Code:
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet
      version="1.0" exclude-result-prefixes="rdf rss l dc admin content xsl"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                      xmlns:rss="http://purl.org/rss/1.0/"
                    xmlns:dc="http://purl.org/dc/elements/1.1/"
                      xmlns:admin="http://webns.net/mvcb/"
                      xmlns:l="http://purl.org/rss/1.0/modules/link/"
                      xmlns:content="http://purl.org/rss/1.0/modules/content/">
        <xsl:output method="html" omit-xml-declaration="yes"/>
        <xsl:template match="/rdf:RDF">
    		<div id="feed">
    		<h2>Latest News</h2>
            <xsl:for-each select="rss:item">
    			<div class="news-piece">
                <h3>           
                        <xsl:value-of select="rss:title"/>        
                </h3> 
    			<h4 class="date"><xsl:value-of select="dc:date"/></h4>   
                <p>        
                    <xsl:value-of disable-output-escaping="yes" select="rss:description"/>  
                </p>    
                </div>    
            </xsl:for-each>
    		</div>    
        </xsl:template>
    </xsl:stylesheet>
    Then perform the transformation with PHP. I grabbed this function from PHP.net:
    PHP Code:
    <?php
    function xml2html($xmldata$xsl)
    {
       
    /* $xmldata -> your XML */
       /* $xsl -> XSLT file */   

       
    $path 'include';
       
    $arguments = array('/_xml' => $xmldata);
       
    $xsltproc xslt_create();
       
    xslt_set_encoding($xsltproc'ISO-8859-1');
       
    $html =
           
    xslt_process($xsltproc'arg:/_xml'"$path/$xsl"NULL$arguments);

       if (empty(
    $html)) {
           die(
    'XSLT processing error: 'xslt_error($xsltproc));
       }
       
    xslt_free($xsltproc);
       return 
    $html;
    }           
    ?>
    Just pass in your XSL and XML files in as parameters to that function, and it will return an HTML string that you can echo out to the browser.

  5. #5
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    umm, its started working again. I dunno what happened but its working. It works when I give the absolute path like http://localhost/myRss.php in the $url, like I refernce it as http://localalhost/feedReader.php?fe...host/myRss.php but if I give it like http://localhost/feedReader.php?feed=myRss.php it doesn't take it, even though they are both in the same directory.
    I wonder why is that??
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  6. #6
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Good Ole Vinnie, thanks. I know XSL(though its not used much since we rarely get work which needs XML/XSL) but that PHP function is great.

    Off Topic:


    All this trouble, just for setting up a Blog!! Yep, I've decided to set up a Blog for myself again(after 4 years) & I'm using bMachine v2.8(http://www.boastology.com/). See it in action at http://www.boastology.com/bmachine/. I downloaded Wordpress but can't get it to work on my local machine & after repeated attempts, I ditched it. Also had a good look at BlogWorksXML(which Marcus here, uses) but I thought that something PHP/mySQL would be great, since it'll help me venture out in the PHP world.
    So got this script, bMachine, & I've fixed it up pretty much according to the way I want it to be, cleaned up HTML & used CSS mostly(doing that for past 2days). A few mods in the code etc. too. I think its satisfactory now, I just have to put it online.

    Was just working on its RSS feed & a way to display it when this problem arised.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •