Jump to content

PHP RSS reader


Recommended Posts

Hi,

I would like to make an RSS feed reader on my site using PHP. I tried using the W3School's tutorial (http://www.w3schools.com/Php/php_ajax_rss_reader.asp), but I don't want users to select an rss feed. I tried to modify the scripts myself, but PHP returned multiple errors. I have tried a few RSS widgets such as Google and Feedwind, but they either didn't display the feed or didn't display the text correctly. I searched Softaculous as well, but the RSS scripts they have doesn't provide the functionality I am looking for.

Link to comment
Share on other sites

Guest Geoff

PHP File:

 

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

//find out which feed was selected
if($q=="RSS")
  {
  $xml=("http://path.to/your/feed.xml");
  }

$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>");
  }

 

HTML file:

 

<html>
<head>
<script type="text/javascript">
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","getrss.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body onload="showRSS('RSS')">
<br />
<div id="rssOutput">RSS-feed will be listed here...</div>
</body>
</html>

Link to comment
Share on other sites

Thanks for your reply! I tried the code(s) above and here is what happened...

 

The HTML file doesn't display my feed staffsites.iceitsupport.net/wheeler_jonathon/index.html

 

and I think it's because PHP returns these errors staffsites.iceitsupport.net/wheeler_jonathon/rss.php

Warning: DOMDocument::load() [domdocument.load]: Empty string supplied as input in /home/iceitsup/public_html/Other/StaffSites/wheeler_jonathon/rss.php on line 12

 

Fatal error: Call to a member function getElementsByTagName() on a non-object in /home/iceitsup/public_html/Other/StaffSites/wheeler_jonathon/rss.php on line 16

Link to comment
Share on other sites

More errors

Warning: DOMDocument::load() [domdocument.load]: Couldn't resolve host name in /home/iceitsup/public_html/Other/StaffSites/wheeler_jonathon/rss.php on line 12

 

Warning: DOMDocument::load(http://news.iceitsupport.net/feeds/posts/default) [domdocument.load]: failed to open stream: operation failed in /home/iceitsup/public_html/Other/StaffSites/wheeler_jonathon/rss.php on line 12

 

Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "http://news.iceitsupport.net/feeds/posts/default" in /home/iceitsup/public_html/Other/StaffSites/wheeler_jonathon/rss.php on line 12

 

Fatal error: Call to a member function getElementsByTagName() on a non-object in /home/iceitsup/public_html/Other/StaffSites/wheeler_jonathon/rss.php on line 16

Link to comment
Share on other sites

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.   Paste as plain text instead

  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...