Thursday, 29 October 2009

How to get Fee Content from other sites using PHP and RSS.

Getting content with RSS Feed.

Having RSS feeds will allow you to get fresh content from other websites without having to visit their websites. This will also be good for your website ranking as your pages will contantly be updated when ever the RSS feed site update there content. Another greate benifit of getting RSS feeds from another website is the fact that you will get a list of fresh content which you do not need to maintain.

Creating Our RSS script.

1. Create a file called rssfeed.php and save this.


2. Open the rssfeed.php file into your editor and copy the script below then save it.



$RSS_Content = array();

function RSS_Tags($item, $type)
{
$y = array();
$tnl = $item->getElementsByTagName("title");
$tnl = $tnl->item(0);
$title = $tnl->firstChild->data;

$tnl = $item->getElementsByTagName("link");
$tnl = $tnl->item(0);
$link = $tnl->firstChild->data;

$tnl = $item->getElementsByTagName("description");
$tnl = $tnl->item(0);
$description = $tnl->firstChild->data;

$y["title"] = $title;
$y["link"] = $link;
$y["description"] = $description;
$y["type"] = $type;

return $y;
}


function RSS_Channel($channel)
{
global $RSS_Content;

$items = $channel->getElementsByTagName("item");

// Processing channel

$y = RSS_Tags($channel, 0);
array_push($RSS_Content, $y);

// Processing articles

foreach($items as $item)
{
$y = RSS_Tags($item, 1);
array_push($RSS_Content, $y);
}
}

function RSS_Retrieve($url)
{
global $RSS_Content;

$doc = new DOMDocument();
$doc->load($url);

$channels = $doc->getElementsByTagName("channel");

$RSS_Content = array();

foreach($channels as $channel)
{
RSS_Channel($channel);
}

}


function RSS_RetrieveLinks($url)
{
global $RSS_Content;

$doc = new DOMDocument();
$doc->load($url);

$channels = $doc->getElementsByTagName("channel");

$RSS_Content = array();

foreach($channels as $channel)
{
$items = $channel->getElementsByTagName("item");
foreach($items as $item)
{
$y = RSS_Tags($item, 1); // get description of article, type 1
array_push($RSS_Content, $y);
}

}

}


function RSS_Links($url, $size)
{
global $RSS_Content;

$page = "
    ";

    RSS_RetrieveLinks($url);
    if($size > 0)
    $recents = array_slice($RSS_Content, 0, $size);

    foreach($recents as $article)
    {
    $type = $article["type"];
    if($type == 0) continue;
    $title = $article["title"];
    $link = $article["link"];
    $page .= "
  • $title
  • \n";

    }

    $page .="
\n";


return $page;

}



function RSS_Display($url, $size)
{
global $RSS_Content;

$opened = false;
$page = "";

RSS_Retrieve($url);
if($size > 0)
$recents = array_slice($RSS_Content, 0, $size);

foreach($recents as $article)
{
$type = $article["type"];
if($type == 0)
{
if($opened == true)
{
$page .="\n";
$opened = false;
}
$page .="";
}
else
{
if($opened == false)
{
$page .= "
    \n";
    $opened = true;
    }
    }
    $title = $article["title"];
    $link = $article["link"];
    $description = $article["description"];
    $page .= "
  • $title";

  • if($description != false)
    {
    $page .= "
    $description";

    }
    $page .= "\n";

    if($type==0)
    {
    $page .="
    ";

    }

    }

    if($opened == true)
    {
    $page .="
\n";

}
return $page."\n";

}


?>


3.
Now to get the RSS Feeds showing on any of your .php pages, copy the code below and place this at the top of the page which you want the feeds to be shown on.

require_once("rssfeed.php");
echo RSS_Display("http://www.websitenohow.com/rss.xml", 25);
?>


note : you will need to change http://www.websitenohow.com/rss.xml to what ever your website rss feed is.

Blog by: http://www.websitenohow.com/php/getting-content-with-rss-feeds.php

credits to scriptol

1 comment:

  1. Been looking for a script like this. thanks

    ReplyDelete