Thursday, December 22, 2011

Display others web contents by rss feed

To do this you should need to create a three files-
1. index.php
2. rss.php
3. feed.js

Code of index.php
[php]
<html>
<head>
<SCRIPT SRC="feed.js"></SCRIPT>
</head>
<body>

<form>
<select onchange="showRSS(this.value)">
<option value="">Select an RSS-feed:</option>
<option value="Google">Google News</option>
<option value="MSNBC">MSNBC News</option>
<option value="bdnews24-bangla">bdnews24-bangla</option>
<option value="bdnews24-english">bdnews24-english</option>
<option value="prothom-alo.com">prothom-alo.com</option>
</select>
</form>
<br />
<div id="rssOutput">RSS-feed will be listed here...</div>
</body>
</html>
[/php]

Code of rss.php

[php]
<?php
//get the q parameter from URL
$q=$_GET["q"];

//find out which feed was selected
if($q=="Google")
{
$xml=("http://news.google.com/news?ned=us&topic=h&output=rss");
}
elseif($q=="MSNBC")
{
$xml=("http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml");
}

elseif($q=="bdnews24-bangla")
{
$xml=("http://rss.bdnews24.com/rss/bangla/home/rss.xml");
}
elseif($q=="bdnews24-english")
{
$xml=("http://rss.bdnews24.com/rss/english/home/rss.xml");
}
elseif($q=="prothom-alo.com")
{
$xml=("http://www.prothom-alo.com/rss");
}


$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

//get elements from "<channel>"
$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;

//output elements from "<channel>"
echo("<p><a href='" . $channel_link
. "'>" . $channel_title . "</a>");
echo("<br />");
echo($channel_desc . "</p>");

//get and output "<item>" elements
$x=$xmlDoc->getElementsByTagName('item');
for ($i=0; $i<=2; $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>");
}
?>
[/php]
Code of feed.js
[php]
function showRSS(str)
{
if (str.length==0)
{
document.getElementById("rssOutput").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("rssOutput").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","rss.php?q="+str,true);
xmlhttp.send();
}
[/php]

1 comment:

  1. I just want to tell you that I am just beginner to weblog and certainly liked this blog. Probably I’m likely to bookmark your blog post . You really come with really good article content. Thanks a bunch for sharing your webpage.

    ReplyDelete

Note: Only a member of this blog may post a comment.