Jump to content

RSS Reader with PHP and AJAX


hitech

Recommended Posts

I am using the following PHP code to parse and show the RSS Feeds:

 

<?php

    // ***** BEGIN ***** get the q parameter from URL  ***** BEGIN *****

    $q=$_GET["q"];//find out which feed was selected

    if($q=="Google")

    {

        $xml=("http://rss.terra.com.br/0,,EI1,00.xml");

    }

    elseif($q=="MSNBC")

    {

        $xml=("http://feeds.folha.uol.com.br/folha/emcimadahora/rss091.xml");

    }

    $xmlDoc = new DOMDocument();

    $xmlDoc->load($xml);

 

    // ***** BEGIN ***** get elements from "<channel>"  ***** BEGIN *****

    $channel=$xmlDoc->getElementsByTagName('channel')->item(0);

    $channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;

    $channel_link = $channel->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;

    $channel_desc = $channel->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;

 

    //***** BEGIN ***** output elements from "<channel>" ***** BEGIN *****

    echo("<p><a href='" . $channel_link . "'>" . $channel_title . "</a>");

    echo("<br />");

    echo($channel_desc . "</p>");

 

    // ***** BEGIN ***** get and output "<item>" elements ***** BEGIN *****

    $x=$xmlDoc->getElementsByTagName('item');

    for ($i=0; $i<=10); $i++)

    {

        $item_title=$x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;

        $item_link=$x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;

        $item_desc=$x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;

        echo ("<p><a href='" . $item_link . "'>" . $item_title . "</a>");

        echo ("<br />");

        echo ($item_desc . "</p>");

        echo("<hr>");

    }

?>

 

In this code, the loop for ($i=0; $i<=10); $i++) count 10 items of the feed.

 

How can I parse all items of the feed? I tryed:

    for ($i=0; $i<=count($x.length); $i++) but doesn´t work.

 

Thanks,

Helcio

From Brasil

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.